I'm working on some regex for a input text field and have had some trouble escaping special characters such as "+" or "?".
I've used these two questions for doing so and they work for a string such as c+ but if I enter c++ I get following error in the console Invalid regular expression: /c++/: Nothing to repeat
Here's the code:
$('input').keyup(function(){
var val = $(this).val().trim().toLowerCase();
// from: https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
//val.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
//from: https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
val = val.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
var re = new RegExp(val, 'ig');
console.log(re);
});
Here is a jsFiddle example of the issue
Thanks