Skip to content

escapeRegExp() function ​

str に含まれる RegExp の特殊文字 ^、$、\、.、*、+、?、(、)、[、]、{、}、| をエスケープし、結果を new RegExp のリテラルパターンとして使えるようにします。

Signature:

typescript
function escapeRegExp(str: string): string;

Example ​

ts
escapeRegExp("[fxts](https://fxts.dev/)"); // "\\[fxts\\]\\(https://fxts\\.dev/\\)"

const re = new RegExp(escapeRegExp("$1.00"));
re.test("price: $1.00"); // true

Open Source Code

Released under the Apache-2.0 License.