1

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Does this take in consideration capital letters?
@detonate: Yep, that is what the i flag is for. You use the same in your code.
@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, It will not let me use other characters after the 1st letter string checkup. Edited the question accordingly now.
Felix, I have no luck with this for some reason.
|
1

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

/^[ab]

then you put the rest of your pattern match.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.