Write a regular expression and see it light up your test text live. Get a plain-English breakdown, a structure diagram, a substitution preview, and a heads-up if the pattern looks prone to catastrophic backtracking. This runs the browser's native JavaScript regex engine, so what you see here is exactly what your JavaScript code will do.
String.prototype.replace. Refer to groups with $1 (numbered) or $<name> (named). $& is the whole match. With the g flag every match is replaced, otherwise only the first.