title: How to find exact substring inside a string using javascript
body: <pre><code>var str = "Hello World +1 new test Good Morning";
  var subStr = "+1 new test";
  </code></pre>
  
  <p>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 '+'.</p>
  
  <p>Below is what I am using -</p>
  
  <pre><code>    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);
  </code></pre>
  
  <p>This works when probStr is a string containing alphanumeric characters but fails when it has special characters e.g. "+", "?"</p>
  
  <p>I found my answer to match exact phrase</p>
  
  <pre><code>        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);
  </code></pre>
  
  <p>This return true when subStr = "+1 new test" and false when subStr = ""+1 new te" (i.e. in case of partial phrase).</p>