Bernardo Siqueira
Bernardo Siqueira

Reputation: 414

Testing string for words with 3 repeated consecutive characters

I'm trying to get a Regex expression that will be satisfied when given a string that has at least 1 word composed of 3 or more repeating consecutive characters, and no other characters:

Testing AAAAAA Test - Valid

Testing AAAAAAB Test - Invalid

The previous solution I had reached was not enough to recognize if there were different characters in the word:

/^(?:(.)(?!\1{2}))+$/gi

This was essentially just testing if the 2 characters after each character are equal to it.

Any ideas?

Upvotes: 0

Views: 50

Answers (2)

Barmar
Barmar

Reputation: 780798

Add word boundaries. And use {2,} to match at least 2 repetitions.

/\b(.)\1{2,}\b/

There's no need for i, since you're not matching letters, so case is irrelevant. And g is not needed when just testing; it's only useful if you're returning all the matches or doing a replacement.

Upvotes: 1

georg
georg

Reputation: 214949

How about

\b(.)\1{2,}\b

which is

\b    word boundary
(.)   something
\1    previous thing...
{2,}  ...twice or more
\b    word boundary

https://regex101.com/r/BKHkOc/3

Upvotes: 2

Related Questions