let name = "dea+hl[]rd";
let text = "This dea+hl[]rd guy is super annoying.";
let escaped = name.replace(/[\\\[.+*?(){|^$]/g, "\\$&");
let regexp = new RegExp("\\b" + escaped + "\\b", "gi");
console.log(text.replace(regexp, "_$&_"));
// → This _dea+hl[]rd_ guy is super annoying.
In this example:
Do you need that 3rd backslash at the beginning?
/[\\\[.+*?(){|^$]/g
https://scrn.es/WU5OBMAXhGTQHmYR6BuVivs.
/[\\[.+*?(){|^$]/g
https://scrn.es/4GiJj6ScfTCD22kmrLY1DhIf i'm following, the first two represent an escaped backslash. Which means the 3rd one is escaping the open bracket. But do you need to do that here?