I have this string:
whatever [that's the pattern] by [pattern again] before [whatever].
I would love to wrap the brackets []
and everthing inside them in a span to be like:
whatever <span>[that's the pattern]</span> by </span>[pattern again]</span>
before <span>[whatever]</span>.
I have tried
re = new RegExp(/\[(.*)\]/);
var newString = oldString.replace(re, "<span>$1</span>");
But this returns:
whatever <span>[that's the pattern] by [pattern again] before [whatever]</span>.
Before anyone shoot me about why shouldn't I use regex to parse HTML, the reason I am doing this is because this string is echoed on the web page and the brackets and the text within has different color. I need to wrap them in a span to be able to style them. If there's a better solution I am open.
Many thanks!