Skip to content

escapeRegExp() function

Escapes the RegExp special characters ^, $, \, ., *, +, ?, (, ), [, ], {, }, and | in str, so the result can be used as a literal pattern inside new RegExp.

Signature:

typescript
declare 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.