I have a question about regexp in javascript.
I want to detect whether a string has a substring which has a repeatitive characters or words.
For example, string "aaaaabcd" has a repeatitive substrings of a or aa
but string "abcdefghij" does not have any repeatitive substring.
I made a RegExp in javascript to detect it.
const written_contents = "aaaaaabcd"
const re = new RegExp("(\w+)\1{3,}", "g")
if (re.test(written_contents) ) {
return "repetition detected."
}
My intentions was detecting 3+ same words or characters are repeated.
Let me explain my logic to reach that Regexp
if string is "aaaaaabc",
\w+ will catch any subset made of 1+ characters like a, aa, aaa, b, c, aaab, aabc, aaabc.
(\w+)\1 \1 points to the 1st parenthesis. Here it is (\w)
And {3, } means \1 is repeated more than 3 times.
I gave "g" option to search the whole string.
Now I expect "aaaaa" is captured because first a is \w, second a is \1, third a to fifth a is {3,} thus "aaaaa" matches.
But the code does not work.
What's wrong?