0
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).

aks
  • 458
  • 1
  • 5
  • 16
  • 1
    `str.indexOf(subStr) > -1` – Satpal Jan 16 '17 at 06:16
  • `+` has a special meaning in regexp you need to escape that using `\+` – Vinay Jan 16 '17 at 06:17
  • I'd suggest just taking a glance at the documentation for [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), and learning those methods, which will help you solve this problem and many more that you will encounter in the future. –  Jan 16 '17 at 06:26
  • IndexOf is giving result as true for partial substring too – aks Jan 16 '17 at 07:41

6 Answers6

1

Use includes() function in ES6, and indexOf() in ES5

includes - will return true if the subStr is in the str.

indexOf - will return -1 if no match found, and a if the subStr was found, will return the index, from where the subStr starts.

Examples

var str = "Hello World +1 new test Good Morning";
var subStr = "+1 new test";

console.log(str.includes(subStr)); // ES6

console.log(str.indexOf(subStr) !== -1); // ES5
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • @Rajesh yes of course – Suren Srapyan Jan 16 '17 at 06:32
  • This is not a duplicate question @Rajesh. My question is to match exact phrase. indexOf, contains, search etc. were not answering my questions – aks Jan 19 '17 at 05:23
  • @aks **exact substring inside a string** what do you mean by this then? Also code shared is also pointing to it. If you think its not duplicate, please share both correct case and incorrect case. That would clarify your requirement. – Rajesh Jan 19 '17 at 05:43
1

You can use includes

var str = "Hello World +1 new test Good Morning";
var subStr = "+1 new test";

console.log(str.includes(subStr));     // ECMAScript 6

console.log(str.includes("anything")); // ECMAScript 6
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

+ is a RegEx token, meaning to match one or more of the preceding token.

You need to escape it to make the RegEx engine treat it literally:

\+1 new test

Although as others have mentioned, you should look at string manipulation rather than RegEx in this case.

heemayl
  • 39,294
  • 7
  • 70
  • 76
0

You don't have to use regex here.

str.indexOf(subStr) >=0

This will return true if substring found

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
0

Since '+' sign is valid regex we will need to escape it using a \ like "\+ ". Here is the working example.

var str = "Hello World +1 new test Good Morning";
var subStr = "\+1 new test"
var bol = str.indexOf(subStr) > -1;
console.log(bol);
Mitesh Pant
  • 524
  • 2
  • 15
0

The includes() method determines whether a string contains the characters of a specified string. This method returns true if the string contains the characters, and false if not. Note: The includes() method is case sensitive.

Example :

var str = "Hello +1 world, welcome to the universe.";
var subStr = str.includes("+1 world");

Where subStr is equal to true

Anurag_Soni
  • 542
  • 2
  • 17