indexOf() method is the most basic one and also has the best browser support. This function
returns the position of the first occurrence of a specified value in a string. If the value is not
found it will return
var str = "The quick brown fox jumps over the lazy dog."; var query = "jump"; console.log(str.indexOf(query)); // 20
In the example above we can see that the term
jump is found at position
20 of the sentence.
search() method matches a regular expression with a string. If the query is not a regular
expression it is automatically converted to
RegExp. This allows some more advanced searching.
var str = "The quick brown fox jumps over the lazy dog."; var query = "jump"; var regex = /jUmPs/gi; // global + case insensitive regex console.log(str.search(query)); // 20 console.log(str.search(regex)); // 20
match() method works the same as the
search() method. It also requires a regular
expression as a parameter but instead returns an
Array containing the entire matched string as
the first element.
var str = "The quick brown fox jumps over the lazy dog."; var query = "jump"; console.log(str.match(query)); // jump
This method is probably the simplest to work with.
true if a string is found
inside another string and
false when it is not found. The method is case sensitive, so make sure
to convert your query using
toLowerCase() for example when using this method.
var str = "The quick brown fox jumps over the lazy dog."; console.log(str.includes("fox")); // true console.log(str.includes("quick red fox")); // false
includes() is not supported in Internet Explorer.