[JavaScript] 每三位數(shù)字加逗號(hào)

// 1,234,567,890
'1234567890'.replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g,'$1,');

注:
(1)(?:x)
Matches 'x' but does not remember the match. The parentheses are called non-capturing parentheses, and let you define subexpressions for regular expression operators to work with. Consider the sample expression /(?:foo){1,2}/. If the expression was /foo{1,2}/, the {1,2} characters would apply only to the last 'o' in 'foo'. With the non-capturing parentheses, the {1,2} applies to the entire word 'foo'.

(2)x(?=y)
Matches 'x' only if 'x' is followed by 'y'. This is called a lookahead. For example, /Jack(?=Sprat)/ matches 'Jack' only if it is followed by 'Sprat'. /Jack(?=Sprat|Frost)/ matches 'Jack' only if it is followed by 'Sprat' or 'Frost'. However, neither 'Sprat' nor 'Frost' is part of the match results.

(3)x(?!y)
Matches 'x' only if 'x' is not followed by 'y'. This is called a negated lookahead. For example, /\d+(?!\.)/ matches a number only if it is not followed by a decimal point. The regular expression /\d+(?!\.)/.exec("3.141") matches '141' but not '3.141'.


參考

Regular Expressions - MDN

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前陣子Eric發(fā)微信給我,就一句話:“我辭職了”。順著劇情我回了一句“為什么?” “早都不想做了,加上有個(gè)同事太T...
    林夏薩摩閱讀 1,226評(píng)論 0 17
  • 人心深不可測(cè),你永遠(yuǎn)不知道你碰到的是魔鬼還是禽獸。
    老落閱讀 157評(píng)論 0 0
  • 最近榆林產(chǎn)婦自殺的事情,讓很多人痛心。輿論基本一邊倒的指責(zé)家屬。我作為順產(chǎn)兩個(gè)孩子的媽媽,我有不同看法,我認(rèn)為醫(yī)院...
    承東媽媽閱讀 1,126評(píng)論 8 5
  • 人生經(jīng)常會(huì)面臨很多選擇,工作有時(shí)間也面臨很多抉擇! 由于公司發(fā)展,現(xiàn)在要物流要擴(kuò)展,面臨要上wms物流管理系統(tǒng),連...
    名揚(yáng)天下99閱讀 239評(píng)論 0 1

友情鏈接更多精彩內(nèi)容