Escape regex string javascript

Declare the escape function at first

RegExp.escape = function(string) {
  return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
And you can search special character like this code
var patern = '^^';
var regex = new RegExp(RegExp.escape(patern));
var text = 'Hello^^';
var match = text.match(regex));
Try to type the input

Try to type the patern with special character

Matched text found:

Posted in . Bookmark the permalink. RSS feed for this post.

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.