Q:

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?'));
0
function escapeRegExp(input) {
	const source = typeof input === 'string' || input instanceof String ? input : '';
	return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
0

New to Communities?

Join the community