I'm trying to write a RegEx that starts with the number 0 or ANY Letter (basically it can't start with 1-9). It also must be 13 characters long
I have this but it does not seem to work:
"^[0][a-zA-Z]{13}"
To match all numbers and letters in JavaScript, we use \w which is equivalent to RegEx \[A-za-z0–9_]\ . To skip all numbers and letters we use \W .
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
As usual, the regex engine starts at the first character: 7. The first token in the regular expression is ^. Since this token is a zero-length token, the engine does not try to match it with the character, but rather with the position before the character that the regex engine has reached so far.
$ means "Match the end of the string" (the position after the last character in the string).
Try this regex instead:
"^[0a-zA-Z].{12}$"
[0a-zA-Z]
means "one character, that is either a 0, one of a-z, or one of A-Z".
.{12}
means twelve more characters, whatever they are.
You are looking for something like this:
^[0\w]\S{12}$
This states the first character must be 0 or a word character. Followed by exactly 12 characters that are NOT whitespace (this allows 0-9, characters and special characters. Repace the \S with anything you like. Such as [0-9\w]
A great playground to test regular expressions is at: http://regexpal.com/ I use it constantly to test regular expressions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With