Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript escape regex

// Vanilla JS
function escapeRegex(string) {
  return string.replace(/[.*+?^${}()|[]]/g, '$&');
}

// Or with npm: npm install escape-string-regexp
const escapeRegex = require('escape-string-regexp');

// Usage:
const regex = new RegExp(escapeRegex('How much $ is that?'));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #escape #regex
ADD COMMENT
Topic
Name
7+2 =