简体   繁体   中英

regex for comma delimited 6-digit values

I'm trying to write a validation javascript regex to check a string is a comma delimited 6-digit string. For example:

123456

OR

123456,123456,123456

but NOT

123456,123

The regex I'm using is ^([0-9]{6})(\,[0-9]{6})* . The problem I'm having is how to get the regex to fail on the last part, where it should fail since 123 is not a 6-digit number. I tired adding a $ to the end of the regex, but then it breaks the whole thing. Can someone help me so that the regex will FAIL unless the string is A) a 6-digit string or B) comma delimited 6-digit strings?

You were close but you needed a $ :

^\d{6}(,\d{6})*$

try with this:

\d{6}(,\d{6}){0,}

In this page, it's work

https://regex101.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM