Reputation: 16693
I want to implement a regular expression check for a string with a character which repeats itself more than twice. I am using ActionScript 3.
i.e.:
koby = true
kobyy = true
kobyyy = false
I tried using
/((\w)\2?(?!\2))+/
but it does not seem to work (using RegExp.test()
)
Upvotes: 1
Views: 170
Reputation: 2359
I also tried this one:
var regExp:RegExp = new RegExp('(\\w)\\1{2}');
trace(!regExp.test('koby'));
trace(!regExp.test('kobyy'));
trace(!regExp.test('kobyyy'));
Upvotes: 1
Reputation: 93086
If you want to invalidate the complete string, when there is a character repeated 3 times, you can use a negative lookahead assertion:
^(?!.*(\w)\1{2}).*
See it here on Regexr.
The group starting with (?!
is a negated lookahead assertion. That means the whole regex (.*
to match the complete string) will fail, when there is a word character that is repeated 3 times in the string.
^
is an anchor for the start of the string.
^ # match the start of the string
(?!.* # fail when there is anywhere in the string
(\w) # a word character
\1{2} # that is repeated two times
)
.* # match the string
Upvotes: 3