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
string.search(regular expression);
Example
Let's find the position of a substring:
let str = 'aaa xax bbb';
let res = str.search(/x.x/);
console.log(res);
The code execution result:
4
See also
-
the
testmethod
that checks a string -
the
matchmethod
that searches for matches in a string -
the
matchAllmethod
that searches for all matches in a string -
the
execmethod
that performs a sequential search -
the
replacemethod
that performs search and replacement -
the
splitmethod
that splits a string -
the
includesmethod
that searches for a given string in the current string