Home

Published

- 1 min read

javascript escape regex

img of javascript escape regex

The solution for this is noted below

javascript escape regex

Solution

   // 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?'))

Try other methods by searching on the site. That is if this doesn’t work