8
I want to mark the triple and quadrupled sequences of a string like this:
$string = "AAACDEAAAABBBUUDD";
And I would like to get as a result the following:
<b>AAA</b>CDE<b>AAAA</b><b>BBB</b>UUDD
8
I want to mark the triple and quadrupled sequences of a string like this:
$string = "AAACDEAAAABBBUUDD";
And I would like to get as a result the following:
<b>AAA</b>CDE<b>AAAA</b><b>BBB</b>UUDD
13
$string = 'AAACDEAAAABBBUUDD';
$bold = preg_replace( '/(.)\1{2,}/', '<b>$0</b>' , $string );
See working on IDEONE
var str = "AAACDEAAAABBBUUDD";
var res = str.replace( /(.)\1{2,}/g, "<b>$&</b>");
See working on IDEONE
(.)
= any character (switch to (\S)
if you don’t want spaces);
\1
= the character referred to above by the first group (.)
;
{2,}
two or more occurrences of the preceding character. If you only want sequences of three or four characters, disregarding five or more, replace with {2,3}
(original plus two equals, or original plus three equals).
That is, with each iterated character, we check if it is followed by 2 more occurrences of the same, totaling the 3 or more desired.
The substitution <b>$0</b>
($&
in JS) takes the whole set found, and adds the Bold.
Browser other questions tagged php javascript
You are not signed in. Login or sign up in order to post.
Thanks for the help!
– Pedro
only complementing. If there are 3 or more spaces, the replacement will also occur. If in any string this may occur I recommend using ER (.(?! s+)) 1{2,} Testing here https://regex101.com/r/UDabwC/1/ @Pedro I recommend reading http://aurelionet.regex/gui/rearvisor.html
– Marcos Xavier
@Marcosxavier is a valid observation, but if he does not want spaces
(\S)\1{2,}
That’s enough: https://regex101.com/r/UDabwC/2 - anyway, I improved the response based on your comment, thank you.– Bacco
@Bacco great, I commented to add even.
– Marcos Xavier