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.