Answers for "regex escape character string in JS"

0

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?'));
Posted by: Guest on July-23-2020
0

javascript escape regex

function escapeRegExp(input) {
	const source = typeof input === 'string' || input instanceof String ? input : '';
	return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
Posted by: Guest on May-01-2020

Code answers related to "regex escape character string in JS"

Code answers related to "Javascript"

Browse Popular Code Answers by Language