字符串的正则表达式模式,如12,345,678,900?

时间:2011-07-04 02:16:21

标签: javascript regex

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

我对正则表达式仍然很陌生,所以任何人都可以帮助我吗?非常感谢提前。

P.S。 任何人都可以建议一些好的资源,比如网站,书籍等,用于学习正则表达式吗?

2 个答案:

答案 0 :(得分:4)

这个正则表达式应该匹配:

\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

答案 1 :(得分:0)

尝试使用http://regexlib.com获取良好示例和指向工具的链接,以帮助您快速了解RegEx

另请尝试使用此正则表达式测试器应用http://www.ultrapico.com/Expresso.htm

我之前使用过的另一个工具http://osherove.com/tools

相关问题