久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <legend id='e4hAP'><style id='e4hAP'><dir id='e4hAP'><q id='e4hAP'></q></dir></style></legend>

        • <bdo id='e4hAP'></bdo><ul id='e4hAP'></ul>
        <tfoot id='e4hAP'></tfoot>

      1. <small id='e4hAP'></small><noframes id='e4hAP'>

      2. <i id='e4hAP'><tr id='e4hAP'><dt id='e4hAP'><q id='e4hAP'><span id='e4hAP'><b id='e4hAP'><form id='e4hAP'><ins id='e4hAP'></ins><ul id='e4hAP'></ul><sub id='e4hAP'></sub></form><legend id='e4hAP'></legend><bdo id='e4hAP'><pre id='e4hAP'><center id='e4hAP'></center></pre></bdo></b><th id='e4hAP'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='e4hAP'><tfoot id='e4hAP'></tfoot><dl id='e4hAP'><fieldset id='e4hAP'></fieldset></dl></div>

        用于在 JavaScript 中格式化數字的正則表達式

        Regular Expression for formatting numbers in JavaScript(用于在 JavaScript 中格式化數字的正則表達式)
          <bdo id='tTkEI'></bdo><ul id='tTkEI'></ul>

          <small id='tTkEI'></small><noframes id='tTkEI'>

            <tbody id='tTkEI'></tbody>

              <tfoot id='tTkEI'></tfoot>

                <i id='tTkEI'><tr id='tTkEI'><dt id='tTkEI'><q id='tTkEI'><span id='tTkEI'><b id='tTkEI'><form id='tTkEI'><ins id='tTkEI'></ins><ul id='tTkEI'></ul><sub id='tTkEI'></sub></form><legend id='tTkEI'></legend><bdo id='tTkEI'><pre id='tTkEI'><center id='tTkEI'></center></pre></bdo></b><th id='tTkEI'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='tTkEI'><tfoot id='tTkEI'></tfoot><dl id='tTkEI'><fieldset id='tTkEI'></fieldset></dl></div>
                <legend id='tTkEI'><style id='tTkEI'><dir id='tTkEI'><q id='tTkEI'></q></dir></style></legend>
                  本文介紹了用于在 JavaScript 中格式化數字的正則表達式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我需要使用 JavaScript 在網頁上顯示格式化的數字.我想格式化它,以便在正確的位置有逗號.我將如何使用正則表達式來做到這一點?我已經做到了這樣的事情:

                  I need to display a formatted number on a web page using JavaScript. I want to format it so that there are commas in the right places. How would I do this with a regular expression? I've gotten as far as something like this:

                  myString = myString.replace(/^(d{3})*$/g, "${1},");
                  

                  ...然后意識到這將比我想象的更復雜(并且上面的正則表達式 甚至沒有接近我需要的).我已經進行了一些搜索,但我很難找到適合這個的東西.

                  ...and then realized this would be more complex than I think (and the regex above is not even close to what I need). I've done some searching and I'm having a hard time finding something that works for this.

                  基本上,我想要這些結果:

                  Basically, I want these results:

                  • 45 變成 45
                  • 3856 變為 3,856
                  • 398868483992 變為 398,868,483,992

                  ...你明白了.

                  推薦答案

                  這可以在單個正則表達式中完成,無需迭代.如果您的瀏覽器支持 ECMAScript 2018,您可以簡單地使用環視并在正確的位置插入逗號:

                  This can be done in a single regex, no iteration required. If your browser supports ECMAScript 2018, you could simply use lookaround and just insert commas at the right places:

                  搜索 (?<=d)(?=(ddd)+(?!d)) 并全部替換為 ,

                  Search for (?<=d)(?=(ddd)+(?!d)) and replace all with ,

                  在舊版本中,JavaScript 不支持后視,因此這不起作用.幸運的是,我們只需要稍作改動:

                  In older versions, JavaScript doesn't support lookbehind, so that doesn't work. Fortunately, we only need to change a little bit:

                  搜索 (d)(?=(ddd)+(?!d)) 并全部替換為 1,

                  Search for (d)(?=(ddd)+(?!d)) and replace all with 1,

                  所以,在 JavaScript 中,它看起來像:

                  So, in JavaScript, that would look like:

                  result = subject.replace(/(d)(?=(ddd)+(?!d))/g, "$1,");
                  

                  說明:斷言從字符串中的當前位置開始,可以匹配三的倍數的數字,并且在當前位置的左邊有一個數字.

                  Explanation: Assert that from the current position in the string onwards, it is possible to match digits in multiples of three, and that there is a digit left of the current position.

                  這也適用于小數 (123456.78),只要點右側"沒有太多數字(否則你會得到 123,456.789,012).

                  This will also work with decimals (123456.78) as long as there aren't too many digits "to the right of the dot" (otherwise you get 123,456.789,012).

                  也可以在 Number 原型中定義,如下:

                  You can also define it in a Number prototype, as follows:

                  Number.prototype.format = function(){
                     return this.toString().replace(/(d)(?=(d{3})+(?!d))/g, "$1,");
                  };
                  

                  然后像這樣使用它:

                  var num = 1234;
                  alert(num.format());
                  

                  學分:Jeffrey Friedl,掌握正則表達式,第 3 期.版, p.66-67

                  Credit: Jeffrey Friedl, Mastering Regular Expressions, 3rd. edition, p. 66-67

                  這篇關于用于在 JavaScript 中格式化數字的正則表達式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browserify, Babel 6, Gulp - Unexpected token on spread operator(Browserify,Babel 6,Gulp - 傳播運算符上的意外令牌)
                  Is it possible to pass a flag to Gulp to have it run tasks in different ways?(是否可以將標志傳遞給 Gulp 以使其以不同的方式運行任務?)
                  Why do we need to install gulp globally and locally?(為什么我們需要在全局和本地安裝 gulp?)
                  How to run Gulp tasks sequentially one after the other(如何一個接一個地依次運行 Gulp 任務)
                  Visual Studio 2015 crashes when opening Javascript files(打開 Javascript 文件時 Visual Studio 2015 崩潰)
                  Detect FLASH plugin crashes(檢測 FLASH 插件崩潰)

                      <bdo id='YlHqu'></bdo><ul id='YlHqu'></ul>
                    • <legend id='YlHqu'><style id='YlHqu'><dir id='YlHqu'><q id='YlHqu'></q></dir></style></legend>

                    • <i id='YlHqu'><tr id='YlHqu'><dt id='YlHqu'><q id='YlHqu'><span id='YlHqu'><b id='YlHqu'><form id='YlHqu'><ins id='YlHqu'></ins><ul id='YlHqu'></ul><sub id='YlHqu'></sub></form><legend id='YlHqu'></legend><bdo id='YlHqu'><pre id='YlHqu'><center id='YlHqu'></center></pre></bdo></b><th id='YlHqu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='YlHqu'><tfoot id='YlHqu'></tfoot><dl id='YlHqu'><fieldset id='YlHqu'></fieldset></dl></div>
                          <tbody id='YlHqu'></tbody>

                            <tfoot id='YlHqu'></tfoot>

                            <small id='YlHqu'></small><noframes id='YlHqu'>

                            主站蜘蛛池模板: 精品一区二区三区在线观看国产 | 人人人人爽| 中文字幕在线一区 | 蜜桃视频一区二区三区 | 欧美性a视频 | 97国产超碰| 久久久国产视频 | 九九国产在线观看 | 视频在线亚洲 | av一级一片 | 91精品国产一区二区三区 | 黄色免费在线观看网站 | 亚洲精品中文字幕在线 | 午夜精品久久久久久久久久久久久 | 国产精品久久国产精品 | 精品欧美一区二区三区免费观看 | 国产区一区二区三区 | 久久91精品国产 | 欧美a级网站 | 国产精品久久久久久久久免费樱桃 | 国产99久久久久 | 精品国产一区二区国模嫣然 | 久久综合国产 | 草久网| 欧美日韩一区二区三区四区 | 91成人免费看 | 啪视频在线 | 日韩一区二区免费视频 | 日韩国产中文字幕 | 国产一区二区三区久久久久久久久 | 精品免费国产视频 | 久久的色| 亚洲伊人a | 日韩在线播放网址 | 欧美自拍日韩 | 国产精品伦理一区二区三区 | 中文字幕亚洲精品 | 日韩精品久久久久 | 国产精品精品久久久 | 九九九精品视频 | 中文字幕亚洲精品 |