javascript - Regular expression for unique number.
737
I need a regular expression that validate an unique value. It has a unique 9 digit number, in the format 000000000A (where 0 is a digit and A is a letter). Only allows letter "V" or "X" for last string.
I can do it for digits but not sure how to modify for last string.
^[1-9][0-9]{9}$
Hope somebody may help me out.
Answer
Solution:
You can use character class
[VX]
, it'll match a single character from it.Or,
OR
condition as followUpdate:
To match the alphabets case-insensitively, use
i
flag or the lowercase characters can also be added in classAnswer
Solution:
You can also try something simpler (unless the leading number cannot be 0)
Answer
Solution:
Why not just
d{9}
?/\d{9}(V|X)/
seems easier.http://jsfiddle.net/u5gmfjeL/1/ - matches
830363670V
as well.case insensitive add
/i
-> /\d{9}(V|X)/i
-> http://jsfiddle.net/u5gmfjeL/2/Answer
Solution:
You could do:
Or more generically if the suffix were more than one character you could use groups. As it appears you are using POSIX Extended Regular Expression (ERE) syntax, you can also use parenthesis for groups, like:
Link to ERE syntax
Link to JavaScript Regex standard