pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="1xlbpth" 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="djhrp5d" 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="rnbtzpr" 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'>

                            主站蜘蛛池模板: 避光流动池-带盖荧光比色皿-生化流动比色皿-宜兴市晶科光学仪器 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 冷凝水循环试验箱-冷凝水试验箱-可编程高低温试验箱厂家-上海巨为(www.juweigroup.com) | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 舞台木地板厂家_体育运动木地板_室内篮球馆木地板_实木运动地板厂家_欧氏篮球地板推荐 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 全自动烧卖机厂家_饺子机_烧麦机价格_小笼汤包机_宁波江北阜欣食品机械有限公司 | 全自动固相萃取仪_高通量真空平行浓缩仪-勤业永为 | 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 杭州顺源过滤机械有限公司官网-压滤机_板框压滤机_厢式隔膜压滤机厂家 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | FAG轴承,苏州FAG轴承,德国FAG轴承-恩梯必传动设备(苏州)有限公司 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 减速机电机一体机_带电机减速器一套_德国BOSERL电动机与减速箱生产厂家 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 粉丝机械,粉丝烘干机,粉丝生产线-招远市远东粉丝机械有限公司 | 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | 锤式粉碎机,医药粉碎机,锥式粉碎机-无锡市迪麦森机械制造有限公司 | 沈阳网站建设_沈阳网站制作_沈阳网页设计-做网站就找示剑新零售 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 鹤壁创新仪器公司-全自动量热仪,定硫仪,煤炭测硫仪,灰熔点测定仪,快速自动测氢仪,工业分析仪,煤质化验仪器 | 实体店商新零售|微赢|波后|波后合作|微赢集团 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 上海办公室设计_办公楼,写字楼装修_办公室装修公司-匠御设计 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 代办建筑资质升级-建筑资质延期就找上海国信启航 | 精密交叉滚子轴承厂家,转盘轴承,YRT转台轴承-洛阳千协轴承 | 大米加工设备|大米加工机械|碾米成套设备|大米加工成套设备-河南成立粮油机械有限公司 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | Honsberg流量计-Greisinger真空表-气压计-上海欧臻机电设备有限公司 | 牛皮纸|牛卡纸|进口牛皮纸|食品级牛皮纸|牛皮纸厂家-伽立实业 | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 首页-瓜尔胶系列-化工单体系列-油田压裂助剂-瓜尔胶厂家-山东广浦生物科技有限公司 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | CPSE安博会| 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 郑州巴特熔体泵有限公司专业的熔体泵,熔体齿轮泵与换网器生产厂家 |