I'm looking to match the following strings so that I can later truncate them from the string in JS. For ex: matched: i like eeeting pie and a a a a a bananas. matched: My name iIIs John Doe and A a A a AA AAA I'm cool.
This is the regex I currently have, but it doesn't match if there is a space in between or if there are more than 3 consecutive letters:
(.)\1\1
You can use
/(\S)(?:\s*\1){2,}/gi
/\b(\S)(?:\s*\1){2,}/gi
If you need to make sure you only start matching at the word boundary, you need to use the second regex.
See the JavaScript demo:
const text = "i like eeeting pie and a a a a a bananas. My name iIIs John Doe and A a A a AA AAA I'm cool.";
const re = /(\S)(?:\s*\1){2,}/gi;
document.body.innerHTML = text.replace(re, '<b>$&</b>')
// Second regex demo:
document.body.innerHTML += "<br/>" + text.replace(/\b(\S)(?:\s*\1){2,}/gi, '<b>$&</b>')
The regex has the i case insensitive flag and matches
(\S) - Group 1: any non-whitespace char(?:\s*\1){2,} - two or more occurrences of
\s* - zero or more whitespaces\1 - the value of Group 1.See the regex scheme: