I have a string in JavaScript in which I\'d like to find all matches of a given phrase and wrap them with a tag. I haven\'t been able to find the right regex method here to
One needs no capturing group around the whole regex because there is a $& placeholder that refers to the whole match from the string replacement pattern:
"Foo bar cat".replace(/cat/ig, "$&");
^^
See the Specifying a string as a parameter section:
$&Inserts the matched substring.
Thus, no need of any callback methods or redundant regex constructs.