javascript construct regexp with string escape code example
Example 1: javascript escape regex
function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
const escapeRegex = require('escape-string-regexp');
const regex = new RegExp(escapeRegex('How much $ is that?'));
Example 2: javascript escape regex
function escapeRegExp(input) {
const source = typeof input === 'string' || input instanceof String ? input : '';
return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}