BACK
TOP
Different ways to check if a string contains a substring in JavaScript
Matías Creimerman - Buenos Aires, Argentina - 2017-03-30

Ways to find a string in another string:

-indexOf:

str.indexOf(searchValue[, fromIndex])

Parameters:

searchValue
value to search for.
fromIndex (Optional)
Index at which to start the searching forwards in the string.
Default is 0.
fromIndex <= 0 entire string is searched.
fromIndex >= str.length string is not searched and -1 is returned.
If searchValue is an empty string, str.length is returned.

Return value:
The index of the first occurrence of the specified value; -1 if not found.

                    
'Blue Whale'.indexOf('Blue');            // returns  0
'Blue Whale'.indexOf('Blute');           // returns -1
'Blue Whale'.indexOf('Whale',0);         // returns  5
'Blue Whale'.indexOf('Whale',5);         // returns  5
'Blue Whale'.indexOf('');                // returns  0
'Blue Whale'.indexOf('', 9);             // returns  9
'Blue Whale'.indexOf('', 10);            // returns 10
'Blue Whale'.indexOf('', 11);            // returns 10
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

-includes:

str.includes(searchString[, position])

Parameters:

searchString
A string to be searched for within this string.
position (Optional)
The position within the string at which to begin searching for searchString.(defaults to 0).

Return value:
true if the given string is found anywhere within the search string; otherwise, false if not.

                    
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be'));                 // true
console.log(str.includes('question'));              // true
console.log(str.includes('nonexistent'));           // false
console.log(str.includes('To be', 1));              // false
console.log(str.includes('TO BE'));                 // false
                    

Note you may need to load es6-shim or similar to get this working on older browsers.

require('es6-shim')

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support 41 (40) No support Yes No support 9

-search:

str.search(regexp)

Parameters:

regexp
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Return value:
The index of the first match between the regular expression and the given string; if not found, -1.

                    
var string = "foo", expr = "/oo/";
string.search(expr);
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

-RegExp test:

regexObj.test(str)

Parameters:

str
The string against which to match the regular expression.

Return value:
true if there is a match between the regular expression and the specified string; otherwise, false.

                    
var string = "foo", expr = /oo/;    //no quotes
expr.test(string);
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

-RegExp Match:

regexp[Symbol.match](str)

Parameters:

str
A String that is a target of the match.

Return value:
An Array containing the entire match result and any parentheses-captured matched results, or null if there were no matches.

                
                var string = "foo",
                        expr = "/oo/";
                string.match(expr);
                    

Browser compatibility:

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (49) No support (Yes) (Yes)
Samples and definitions are from:
https://developer.mozilla.org/en-US/
Thanks Mozilla!
This content is property of Matias Creimerman
Any misuse of this material will be punishable
This work is licensed under a
Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
Creative Commons License