Question:
How to check whether a string has a substring in JavaScript?
Answer:
There are few ways to solve this problem. See below for more details.
One: use indexOf
| 12
 3
 
 | var string = "foo",substring = "oo";
 string.indexOf(substring) !== -1;
 
 | 
indexOf will find the index position of this substring, if not found, will return -1
Two: use includes
| 12
 3
 
 | var string = "foo",substring = "oo";
 string.includes(substring);
 
 | 
Three: use search
| 12
 3
 
 | var string = "foo",expr = /oo/;
 string.search(expr);
 
 | 
Four: use lodash includes
| 12
 3
 
 | var string = "foo",substring = "oo";
 _.includes(string, substring);
 
 | 
Five: use RegExp
| 12
 3
 
 | var string = "foo",expr = /oo/;
 expr.test(string);
 
 | 
Six: use Match
| 12
 3
 
 | var string = "foo",expr = /oo/;
 string.match(expr);
 
 | 
Reference
This is the end of post