跳到主要内容

escapeRegExp

TS JS Deno

Escapes a string to use in a regular expression.

Use String.prototype.replace() to escape special characters.

typescript
const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
typescript
escapeRegExp("(test)"); // \\(test\\)