I managed to create a regex that works, but I believe there may be a better use-case:
element = '<div style="color:red">123</div>';
element.replace(/(<div.*>)(\d+)(<\/div>)/g, '$1<b>$2</b>$3');
// expecting output: <div style="color:red"><b>123</b></div>
After doing some research, I discovered that (?: ... )
in regex means to ignore group matches, like this:
element.replace(/(?:<div.*>)(\d+)(?:<\/div>)/g, '<b>$1</b>');
// returns <b>123</b>
However, I still want my expected result from the 1st example.
Is there a way to exclude them? Can we simply write
replace(/.../, '<b>$1</b>')
?
This is just a simple case to understand how to exclude groups in regex. And yes, I'm aware that we can't parse HTML with regex :)