var haystack = "A. BAIL. Of. Hay."; var needle = "bail."; var needleRegExp = new RegExp(needle.replace(/[-[]{}()*+?.,^$|#s]/g, "$&"), "i"); var result = needleRegExp.test(haystack); if (result) { // Your code here }