detonate
detonate

Reputation: 289

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.

mystring.match(/^D\d{22}$/i

Upvotes: 1

Views: 113

Answers (3)

Felix Kling
Felix Kling

Reputation: 816532

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.

Upvotes: 1

Nash
Nash

Reputation: 531

This should work "/^[ab][A-Za-z0-9_-]*$" for generic string to start with "a" or "b" use link to verify RegEx online.

Upvotes: 1

Mat
Mat

Reputation: 206727

"Start with a or b" would be in regex-speech:

/^[ab]

then you put the rest of your pattern match.

Upvotes: 1

Related Questions