1const string = "javascript";
2const substring = "script";
3
4console.log(string.includes(substring)); //true
1var string = "foo",
2 substring = "oo";
3
4console.log(string.includes(substring));
1const string = "foo";
2const substring = "oo";
3
4console.log(string.includes(substring));
1// With ES6 MDN docs .includes()
2"FooBar".includes("oo"); // true
3"FooBar".includes("foo"); // false
4"FooBar".includes("oo", 2); // false (2 is the start position for the search)
5
6// E: Not suported by IE - instead you can use the Tilde opperator ~ (Bitwise NOT) with .indexOf()
7~"FooBar".indexOf("oo"); // -2
8~"FooBar".indexOf("foo"); // 0
9~"FooBar".indexOf("oo", 2); // 0 (parameter 2 is the start position for the search)
10
11// Used with a number, the Tilde operator effective does ~N => -(N+1). Use it with double negation !! (Logical NOT) to convert the numbers in bools:
12!!~"FooBar".indexOf("oo"); // true
13!!~"FooBar".indexOf("foo"); // false
14!!~"FooBar".indexOf("oo", 2); // false
1var str = "foobar"
2var regex = /foo/g;
3if (str.search(regex) !== -1) {
4 alert("string conains foo!")
5}