Loading...
const re = new RegExp(query, 'gi');
const html = text.replace(re, '<mark>$&</mark>');const re = new RegExp(RegExp.escape(query), 'gi');
const html = text.replace(re, '<mark>$&</mark>');Unescaped input like 'a.b' or 'c++' changes the pattern or throws; RegExp.escape() makes it match literally.
RegExp.escape() before it goes into new RegExp().includes() instead of a regular expression.In older browsers, yes. Feature-detect it with typeof RegExp.escape === 'function' and fall back to a small replace-based escape helper.
Yes, for plain text, str.replaceAll(query, value) with a string pattern needs no escaping at all.