Skip to content Skip to sidebar Skip to footer

Escape A '+' Plus Sign In A String To Be Used In A Regex In Coffeescript/javascript

I have a regex I'm running to filter rows in a table. The filtering is done in Javascript. I'm writing coffeescript, but a Javascript solution would be fine -- I can just translate

Solution 1:

+ is far from the only character with special meaning. Here is a function that will escape all the necessary characters:

function regex_escape(str) {
    return str.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g'), '\\$&');
}

Post a Comment for "Escape A '+' Plus Sign In A String To Be Used In A Regex In Coffeescript/javascript"