从标题可以看出,我想写一个正则表达式模式来查找由各种数字组成的字符串,每三位数字用逗号分隔。字符串的长度可以变化。
我对正则表达式还是很陌生所以有人可以帮助我吗?非常感谢。
附言 任何人也可以推荐一些学习正则表达式的好资源,如网站、书籍等?
最佳答案
此正则表达式应匹配:
\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/