Skip to content

Commit

Permalink
Tipify escape-reg-exp
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Jun 2, 2024
1 parent c9151f8 commit 56f7080
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions content/snippets/js/s/escape-reg-exp.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
---
title: Escape RegExp
type: snippet
title: Escape a regular expression in JavaScript
shortTitle: Escape RegExp
type: tip
language: javascript
tags: [string,regexp]
cover: frog-blue-flower
dateModified: 2020-09-15
excerpt: Learn how to escape a string to use in a regular expression.
dateModified: 2024-05-29
---

Escapes a string to use in a regular expression.
Regular expressions are a powerful tool for pattern matching and string manipulation. However, when you need to use a string as a regular expression, you need to **escape special characters** to avoid syntax errors.

- Use `String.prototype.replace()` to escape special characters.
Luckily, escaping a string for use in a regular expression is not hard, but it requires, you guessed it, a regular expression! By using the `String.prototype.replace()` method, you can escape special characters in a string.

The **regular expression** that you can then use to escape special characters is `/[.*+?^${}()|[\]\\]/g`. This regular expression matches all the special characters used in regular expressions. Then, each match is replaced with the **escaped version of the character** using `\\$&`.

```js
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
Expand Down

0 comments on commit 56f7080

Please sign in to comment.