Escape RegExp

JavaScript, String, Regexp · Sep 15, 2020

Escapes a string to use in a regular expression.

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

More like this

  • Common regular expressions

    A collection of regular expressions that can be used to solve common problems.

    JavaScript, String · Nov 9, 2022

  • Escape HTML

    Escapes a string for use in HTML.

    JavaScript, String · Oct 13, 2021

  • Regular Expressions Cheat Sheet

    Regular expressions are a very useful tool. Save this cheatsheet for when you need to look up their syntax and speed up your development.

    JavaScript, String · Jun 12, 2021