0

How to escape all special charactors (modifiers) in the pattern?

In this snippet of code the var input contains a modifier *.. How to escape all special characters in the input (pattern) variable?

var input = '*dog',
    regexp = new RegExp(input, 'ig'),
    str = 'a *dog and a cat';

if(str.search(regexp) >= 0){
    console.log('found!');
}
else{
    console.log('not found!');
}

http://jsfiddle.net/8511wfbk/1/

clarkk
  • 27,151
  • 72
  • 200
  • 340

0 Answers0