Reputation: 1639
I need to determine if a string contains two or more consecutive alpha chars. Two or more [a-zA-Z]
side by side.
Example:
"ab" -> valid
"a1" -> invalid
"a b" -> invalid
"a"-> invalid
"a ab" -> valid
"11" -> invalid
Upvotes: 162
Views: 172469
Reputation: 71
Personally I've used:
[0-9][0-9]+.
But the answer from Simon, is way better.
Upvotes: 7
Reputation: 191
[a-zA-Z]{2,}
does not work for two or more identical consecutive characters. To do that, you should capture any character and then repeat the capture like this:
(.)\1
The parenthesis captures the . which represents any character and \1
is the result of the capture - basically looking for a consecutive repeat of that character. If you wish to be specific on what characters you wish to find are identical consecutive, just replace the "any character" with a character class...
([a-zA-Z])\1
Finds a consecutive repeating lower or upper case letter. Matches on abbc123
and not abc1223
. To allow for a space between them (i.e. a ab
), then include an optional space in the regex between the captured character and the repeat...
([a-z]A-Z])\s?\1
Upvotes: 18
Reputation: 465
I'm pretty sure you can just use [A-z] instead of the [a-zA-Z] to get small and upper case alpha chars http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Upvotes: -7