Files
30-seconds-of-code/snippets/escapeRegExp.md
2020-09-15 21:52:00 +03:00

17 lines
309 B
Markdown

---
title: escapeRegExp
tags: string,regexp,intermediate
---
Escapes a string to use in a regular expression.
- Use `String.prototype.replace()` to escape special characters.
```js
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
```
```js
escapeRegExp('(test)'); // \\(test\\)
```