久久久久久久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 中格式化數(shù)字的正則表達(dá)式

        Regular Expression for formatting numbers in JavaScript(用于在 JavaScript 中格式化數(shù)字的正則表達(dá)式)
          <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 中格式化數(shù)字的正則表達(dá)式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

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

                  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},");
                  

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

                  ...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.

                  基本上,我想要這些結(jié)果:

                  Basically, I want these results:

                  • 45 變成 45
                  • 3856 變?yōu)?3,856
                  • 398868483992 變?yōu)?398,868,483,992

                  ...你明白了.

                  推薦答案

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

                  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,");
                  

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

                  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.

                  這也適用于小數(shù) (123456.78),只要點右側(cè)"沒有太多數(shù)字(否則你會得到 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());
                  

                  學(xué)分:Jeffrey Friedl,掌握正則表達(dá)式,第 3 期.版, p.66-67

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

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

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

                  相關(guān)文檔推薦

                  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?(是否可以將標(biāo)志傳遞給 Gulp 以使其以不同的方式運行任務(wù)?)
                  Why do we need to install gulp globally and locally?(為什么我們需要在全局和本地安裝 gulp?)
                  How to run Gulp tasks sequentially one after the other(如何一個接一個地依次運行 Gulp 任務(wù))
                  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'>

                            主站蜘蛛池模板: 丁香婷婷成人 | 色狠狠一区 | 粉嫩av久久一区二区三区 | 亚洲黄色视屏 | 日韩精品一区二区三区中文在线 | 久久综合九色综合欧美狠狠 | 国产精品久久二区 | 欧美国产视频一区二区 | 国产一区二区三区免费 | 亚洲aⅴ | 在线一区二区三区 | 午夜精品福利视频 | 91精品综合久久久久久五月天 | 一区中文字幕 | 成人黄页在线观看 | av免费入口 | 欧美涩涩网 | 国产一区高清 | 精品国产欧美一区二区 | 亚洲入口| a在线免费观看 | 日韩中文电影 | 久久久免费 | 一区二区三区免费观看 | 中文字幕在线观看成人 | 欧美日韩在线一区二区 | 狠狠做深爱婷婷综合一区 | 日本一区二区三区视频在线 | 午夜精品在线观看 | 一级a性色生活片久久毛片波多野 | h视频在线免费 | 正在播放亚洲 | 久久99视频这里只有精品 | 日韩欧美国产综合 | 久久99久久99久久 | 欧美激情在线精品一区二区三区 | 欧美亚洲视频 | 国产精品久久久久久久久久久久久 | av在线电影网站 | www.久久.com | 日韩精品1区2区3区 成人黄页在线观看 |