[JavaScript] 每三位数字加逗号

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

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前阵子Eric发微信给我,就一句话:“我辞职了”。顺着剧情我回了一句“为什么?” “早都不想做了,加上有个同事太T...
    林夏萨摩阅读 1,264评论 0赞 17
  • 人心深不可测,你永远不知道你碰到的是魔鬼还是禽兽。
    老落阅读 164评论 0赞 0
  • 最近榆林产妇自杀的事情,让很多人痛心。舆论基本一边倒的指责家属。我作为顺产两个孩子的妈妈,我有不同看法,我认为医院...
    承东妈妈阅读 1,166评论 8赞 5
  • 人生经常会面临很多选择,工作有时间也面临很多抉择! 由于公司发展,现在要物流要扩展,面临要上wms物流管理系统,连...
    名扬天下99阅读 271评论 0赞 1

友情链接更多精彩内容