-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
  • 211,518
  • 52
  • 313
  • 390
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
  • 58,320
  • 7
  • 77
  • 95
1

Use this regex instead:

^[UC].{9}$

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

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260