How to use the escape function from xregexp

Find comprehensive JavaScript xregexp.escape code examples handpicked from public code repositorys.

136
137
138
139
140
141
142
143
144
145


// See http://eloquentjavascript.net/09_regexp.html Dynamically creating RegExp objects
let name = "dea+hl[]rd";
let text = "This dea+hl[]rd guy is super annoying.";
let escaped = XRegExp.escape(name);
let regexp = new RegExp("\\b" + escaped + "\\b", "gi");
console.log(text.replace(regexp, "_$&_"));
// → This _dea+hl[]rd_ guy is super annoying.
//
fork icon1
star icon0
watch icon2