JavaScript regex – How to wrap matches with tag?

You could use:

"Foo bar cat".replace(/(cat)/ig, "<em>$1</em>");

Which will return:

"Foo bar <em>cat</em>"

Leave a Comment