-3

I need javascript regular expression to check string starting with U or C. Also it should be of length 10.

I tried this ^U|C{9}$ but not getting proper results.

georg
  • 204,715
  • 48
  • 286
  • 369
prashant
  • 11
  • 5

2 Answers2

1

You should group the tokens when using | operator, so as to get intended results. Also, you must use .(any character except new-line) and make sure it is 9 characters long.

You can also use character class as in /^[UC].{9}$/

/^(U|C).{9}$/

You can also use simple javascript to do this

var chr = str.charAt(0);
if((chr == "U" || chr == "C") && str.length == 10){
   // valid
}
Amit Joki
  • 56,285
  • 7
  • 72
  • 91
1

Use this regex instead:

^[UC].{9}$

The . will match any character, which you missed out.

Samuel Liew
  • 72,637
  • 105
  • 156
  • 238