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
share|improve this question

70% accept rate
feedback

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
Does this take in consideration capital letters? – detonate Mar 8 '11 at 22:34
@detonate: Yep, that is what the i flag is for. You use the same in your code. – Felix Kling Mar 8 '11 at 22:41
@detonate: Or if you don't want capital letters to match, then you have to remove the i. Unfortunately your comment is not clear enough in this case. – Felix Kling Mar 8 '11 at 22:48
Felix, It will not let me use other characters after the 1st letter string checkup. Edited the question accordingly now. – detonate Mar 8 '11 at 22:50
@detonate: Ah stupid mistake of mine... updated my answer. – Felix Kling Mar 8 '11 at 22:56
show 4 more comments
feedback

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

share|improve this answer
feedback

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

/^[ab]

then you put the rest of your pattern match.

share|improve this answer
Edited my question. – detonate Mar 8 '11 at 22:48
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.