/ Published in: JavaScript
Escape all special regex characters (.*+?|()[]{}\) from a string. Useful when dynamically building a Regular Expression object based on input text that could hold regex characters.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
RegExp.escape = function(str) { var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\ return str.replace(specials, "\\$&"); }
URL: http://stackoverflow.com/questions/280793/case-insensitive-string-replacement-in-javascript