Files
30-seconds-of-code/snippets/escapeRegExp.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

16 lines
306 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\\)
```