Reputation: 2926
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.
Upvotes: 4
Views: 2083
Reputation: 2316
You could do:
^[1-9][0-9]{8}[VX]$
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:
^[1-9][0-9]{8}(V|X)$
Link to JavaScript Regex standard
Upvotes: 0
Reputation: 87203
You can use character class [VX]
, it'll match a single character from it.
^[1-9][0-9]{8}[VX]$
Or, OR
condition as follow
^[1-9][0-9]{8}(V|X)$
Update:
To match the alphabets case-insensitively, use i
flag or the lowercase characters can also be added in class
^[1-9][0-9]{8}[VvXx]$
Upvotes: 2
Reputation: 85538
Why not just d{9}
? /\d{9}(V|X)/
seems easier.
var test = "qwerty000000000Xqwerty";
console.log(test.match(/\d{9}(V|X)/));
http://jsfiddle.net/u5gmfjeL/1/ - matches 830363670V
as well.
case insensitive add /i
-> /\d{9}(V|X)/i
-> http://jsfiddle.net/u5gmfjeL/2/
Upvotes: 1
Reputation: 31654
You can also try something simpler (unless the leading number cannot be 0)
\d{9}[VX]
Upvotes: 1