JavaScript String Search


String Search Methods

  • String indexOf()
  • String lastIndexOf()
  • String search()
  • String match()
  • String matchAll()
  • String includes()
  • String startsWith()
  • String endsWith()

JavaScript String indexOf()

The indexOf() function tells you the position of the initial appearance of one string in another string.

Note : JavaScript counts positions from zero.

0 is the first position in a string, 1 is the second, 2 is the third, ...


JavaScript String lastIndexOf()

The lastIndexOf() function tells you where the final or last appearance of a particular word or phrase is in a piece of text.

Both indexOf() and lastIndexOf() functions will give you -1 if the text is not found.

Both ways allow you to specify a starting position for the search by using a second parameter.

The lastIndexOf() function search from the end to the start. Imagine this: If you set the second parameter as 15, it begins searching at position 15 and moves toward the beginning of the string.


JavaScript String search()

The search() function looks for a specific word or pattern within a text, and it tells you where it's found.


Did You Notice?

The two techniques, indexOf() and search(), are they the same?

They accept the same arguments (parameters), and return the same value?

The two ways are not the same. Here are the distinctions:

  • The search() method cannot take a second start position argument.
  • The indexOf() method cannot take powerful search values (regular expressions).

In a future section, you'll gain a better understanding of regular expressions.


JavaScript String match()

The match() function gives you an array with the findings when you compare one string with another string or a pattern.

Note : Without the g modifier (global search) in a regular expression, match() will only find and return the first match in the string.

Read more about regular expressions in the chapter JS RegExp.


JavaScript String matchAll()

The matchAll() function gives you a way to find and list all the matches when you compare one string with another string or a regular expression.

If you use a regular expression as a parameter, make sure to set the global flag (denoted by "g"). If you don't do this, it will result in an error of type "TypeError."

If you want to search case insensitive, the insensitive flag (i) must be set:

Notes : matchAll() is an ES2020 feature.

matchAll() does not work in Internet Explorer.


JavaScript String includes()

The includes() method returns true if a string contains a specified value.

If not, it will give you "false."

Notes : includes() is case sensitive.

includes() is an ES6 feature.

includes() is not supported in Internet Explorer.


JavaScript String startsWith()

The startsWith() function tells us if a string starts with a particular value, and it returns true if it does.

Otherwise it returns false:

A start position for the search can be specified:

Notes : startsWith() is case sensitive.

startsWith() is an ES6 feature.

startsWith() is not supported in Internet Explorer.


JavaScript String endsWith()

The endsWith() function tells us if a string finishes with a particular value, and it gives back true if it does.

Otherwise it returns false:

Notes : endsWith() is case sensitive.

endsWith() is an ES6 feature.

endsWith() is not supported in Internet Explorer.