views:

75

answers:

4
+2  Q: 

Javscript REGEX

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

+3  A: 

This should do it:

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

or

/^a\d{8}$/i
cletus
how do i make sure the 8 characters are digits.. i think its \d or something no?
Rabbott
Or `/^a\d{8}$/i`
Jakub Hampl
+12  A: 

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

Also makes sure the x's are digits :)

David Titarenco
Shouldn't that be `[0-9]`?
Nick Presta
whoops :O yes it should
David Titarenco
A: 

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}/
Eineki
+2  A: 

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.

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 :/
Rabbott