var str = "Hello World +1 new test Good Morning";
var subStr = "+1 new test";
I want the code snippet to return true only when entire phrase i.e. "+1 new test" is found in the string str. I have tried regEx and match,search,test options. But, none are working for me. It shows error for the '+'.
Below is what I am using -
var HVal = "Hello World +1 Good"
var HValNew = HVal.toString().toUpperCase();
var probStr = "Hello World";
var test = probStr.toUpperCase();
var re = new RegExp('\\b'+test+'\\b');
re.test(HValNew);
This works when probStr is a string containing alphanumeric characters but fails when it has special characters e.g. "+", "?"
I found my answer to match exact phrase
var str = "Hello World +1 new test Good Morning".toString().toUpperCase();
var subStr = "+1 new test".toString().toUpperCase();
escapeSpecialChars = function(string) {return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, 'x')};
var probStr = escapeSpecialChars(subStr);
var strNew = escapeSpecialChars(str);
var re = new RegExp("\\b"+probStr+"\\b");
re.test(strNew);
This return true when subStr = "+1 new test" and false when subStr = ""+1 new te" (i.e. in case of partial phrase).