Javascript string verification using Regex
How can I format this so it only checks if my string starts with the letter "a" or "b" and also accepts misc. characters after this (numbers or letters)?
Just need to make sure it starts with the letter a or b, the rest is ok and will have more characters.mystrin开发者_开发知识库g.match(/^D\d{22}$/i
If you only want to match whether the string starts with a
or b
, it would be a completely different expression:
/^[ab][a-z\d]*/i.test(mystring)
Edit: Updated to much arbitrary characters afterwards.
Edit2: Restricted to numbers and letters.
Of course you can also combine this with any other expression if you want to.
This is great source to learn about regular expressions.
"Start with a or b" would be in regex-speech:
/^[ab]
then you put the rest of your pattern match.
This should work "/^[ab][A-Za-z0-9_-]*$" for generic string to start with "a" or "b" use link to verify RegEx online.
精彩评论