I am trying to do a modify a text which contain some special tags. I believe it should be possible by a single regular expression, but am stuck…
What I want to do is easier to explain by an example:
If I have this text:
{Lorem Ipsum} is simply {dummy text} of
the printing and typesetting industry.
I want as a result this:
<span>Lorem Ipsum</span> is <span>simply dummy</span> text of the printing and typesetting industry.
I a } is encountered with no previous matching { then it should be ignored.
I know I can match all inner texts with this \{(.*?)\} but am lost on how to proceed, any help would be appriciated.
You are close.
should do the trick. The
gmodifier makesreplaceto replace every occurrence of the pattern and$1refers to the content of the first capture group.Btw. escaping the
{}is not necessary here as{(.*?)}is not a special construct. It still might be better to escape them for clarity.More about regular expression in JavaScript in the MDN documentation.