Escape RegExp
Escapes a string to use in a regular expression.
- Use
String.prototype.replace()
to escape special characters.
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); escapeRegExp('(test)'); // \\(test\\)
Escapes a string to use in a regular expression.
String.prototype.replace()
to escape special characters.const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); escapeRegExp('(test)'); // \\(test\\)
A collection of regular expressions that can be used to solve common problems.
Learn how to use regular expressions to replace the last occurrence of a pattern in a JavaScript string.
Learn how to convert text into HTML-safe strings and vice versa using JavaScript.
Regular expressions are a very useful tool. Save this cheatsheet for when you need to look up their syntax and speed up your development.