Whenever you use search() on a string, the method does the following:
- It searches its calling string for the method's regular expression argument.
- It returns the index position of the first match, or
-1if the RegExp pattern found no match.
A calling string is a string on which you used
search(). So, in
"Hello, world!" is the calling string.
Syntax of the
search() accepts a regular expression argument. Here is the syntax:
search()'s argument is a non-
new RegExp(...)syntax to convert the argument to a regular expression.
0returns if you do not provide an argument.
Example 1: Search for
"SunDay, Tuesday, and Friday are good DAYS".search(/day/);
// The invocation above will return: 12
Example 2: Search for a Case-Insensitive
"SunDay, Tuesday, and Friday are good DAYS".search(/day/i);
// The invocation above will return: 3
search() searches its calling string for the first occurrence of the method's regular expression argument.