If you type special characters like [ (hard bracket) then regexps will fail.
This function will prevent this error:
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
Use it when you do text.search and when you call new RegExp() and you’ll be fine.