search method

The search method finds string matches with regular expression and returns the position of the first match. If no matches are found, then the method will return -1.

Syntax

javascriptstring.search(regular expression);

Example

Let's find the position of a substring:

javascriptlet str = 'aaa xax bbb';
let res = str.search(/x.x/);

console.log(res);

The code execution result:

javascript4

See also

  • the test method
    that checks a string
  • the match method
    that searches for matches in a string
  • the matchAll method
    that searches for all matches in a string
  • the exec method
    that performs a sequential search
  • the replace method
    that performs search and replacement
  • the split method
    that splits a string
  • the includes method
    that searches for a given string in the current string