i

JavaScript

search()

Another method that is used to search for a string in a string is search().

Example:

var string = “Jake, John, Jake, Joe”;

var position = string.search(“Jake”);

The variable position will have the value 0, the index of the first occurrence of the search string “Jake”.

At a higher level, indexOf() and search() may seem equal, but there are differences among them:

  • search() cannot take the start position for search

  • indexOf() cannot take complex search values like regular expressions