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\\)