Escapes a string to use in a regular expression.
Use String.prototype.replace()
to escape special characters.
代码片段
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
使用样例
escapeRegExp('(test)'); // \\(test\\)
Escapes a string to use in a regular expression.
Use String.prototype.replace()
to escape special characters.
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
escapeRegExp('(test)'); // \\(test\\)