4

I need a javascript REGEX to check that the length of the string is 9 characters. Starts with 'A' or 'a' and is followed by 8 digits.

Axxxxxxxx or axxxxxxxx

0

4 Answers 4

12

/^[aA][0-9]{8}$/ or /^[aA]\d{8}$/

Also makes sure the x's are digits :)

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

Comments

3

This should do it:

/^[aA]\d{8}$/

or

/^a\d{8}$/i

1 Comment

how do i make sure the 8 characters are digits.. i think its \d or something no?
2

This is probably what you want.

/^([aA]\d{8})$/

The carot character means the regex must start searching from the beginning of the string, and the dollar character means the regex must finish searching at the end of the string. When they are used together it means the string must be searched from start to end.

The square brackets are used to specific a character or a range of allow characters. The slash and d means to search any digit character. The brackets at the end specify a static quantity that applies to the previous test definition. A range of quantities can be used by specifing a minimum value immediately followed by a comma immediately followed by a maximum value.

1 Comment

I appreciate the information! I almost always know exactly what the regex is doing when i see it, but I am not able to do it from scratch on my own :/
0

did you mean this?

/^[aA]\d{8}/

or did you mean 9 chars ?

/^[aA]\d{8}/

or did you mean A + 8 equal chars ?

/[aA](.)\1{7}/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.