Trying to escape special characters. I type $ or ^ or | it works fine. Why is my below code not escaping () {} ? * + and \ when I type these I get invalid expression error.
escape = function(value) {
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
}
something wrong with the above expression???
MDN suggests this
function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
even this I'm facing the same issue. no error for {} $ ^ | but I get error for () * ? \ +
[ ](which means they are part of a character class), if the were outside they would need to be escaped to be matched as normal characters.