javascript - 像 12,345,678,900 这样的字符串的正则表达式模式?

标签 javascript regex

从标题可以看出,我想写一个正则表达式模式来查找由各种数字组成的字符串,每三位数字用逗号分隔。字符串的长度可以变化。

我对正则表达式还是很陌生所以有人可以帮助我吗?非常感谢。

附言 任何人也可以推荐一些学习正则表达式的好资源,如网站、书籍等?

最佳答案

此正则表达式应匹配:

\d{1,3}(?:,\d{3})*

如果你想排除与格式错误模式的子串的匹配,你可能想要这样做:

(?:\A|[^,\d])(\d{1,3}(?:,\d{3})*)(?:\z|[^,\d])

第一个正则表达式的解释

\d{1,3}        1 to 3 consecutive numerals
,\d{3}         A comma followed by 3 consecutive numerals
(?:,\d{3})*    Zero or more repetition of a non-capturing group of a comma followed by 3 consecutive numerals

第二个正则表达式的解释

(?:\A|[^,\d])         A non-capturing group of either the beginning of the string, or anything other than comma or numeral
(\d{1,3}(?:,\d{3})*)  A capturing group of 1 to 3 consecutive numerals followed by zero or more repetition of a non-capturing group of a comma followed by 3 consecutive numerals
(?:\z|[^,\d])         A non-capturing group of either the end of the string, or anything other than comma of numeral

关于javascript - 像 12,345,678,900 这样的字符串的正则表达式模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6566768/

相关文章:

javascript - 为什么 jQuery 开/关方法的行为与外部脚本不同

javascript - 拥有多个具有相同悬停效果的图像

javascript - jQuery DataTable 选项名称中的匈牙利符号有什么用?

c# - RegEx替换C#中字符串中的特定字母

正则表达式匹配ababba等字符串

javascript - RegEx:定义一些替换的异常(exception)情况

javascript - jquery 中 get 方法上设置 div 失败

javascript - 在 ZingChart 上自定义 x 轴

regex - 如何仅单击一个类中的链接

javascript - 使用 css 类和正则表达式将样式动态应用到文本部分