So currently I've got the following regex pattern, allowing me to detect any string containing 9 characters that are the same consecutively.
/^.*(\S)\1{9,}.*$/
This works perfectly with a string like the following: this a tesssssssssst
however I wish for it to also detect a string like this: this a tess sss ssssst
(Same number of the repeated character, but with optional whitespace)
Any ideas?
You need to put the backreference into a group and add an optional space into the group:
^.*(\S)(?: ?\1){9,}.*$
See the regex demo. If there can be more than 1 space in between, replace ?
with *
.
The .*$
part is only needed if you need to get the whole line match, for methods that allow partial matches, you may use ^.*(\S)(?: ?\1){9,}
.
If any whitespace is meant, replace the space with \s
in the pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With