The regular expression search () function:
This function can retrieve substrings that match a regular expression.
The return value is the starting position of the first substring that matches the regular expression, and the position is calculated starting at 0. Returns-1 if not found.
This is only a section of the search () method for a string object that directly retrieves sub-characters, which are related to the regular expression.
Syntax structure:
Stringobject.search (RegExp)
The parameter list is as follows:
RegExp required. The regular Expression object.
Browser support:
This method is supported by IE browser.
This method is supported by the Firefox browser.
Google Chrome supports this method.
Instance code:
Example one:
var str= "I love Antzone"; var reg=/ant/ Console.log (Str.search (reg));
The above code can return the starting position of the substring "Antzone" in the string where the regular expression matches.
Example two:
var str= "I love Antzone"; var reg=/abc/ Console.log (Str.search (reg));
There are no strings in the string that the regular expression will match, and this function returns-1.
The original address is: Regular expression search () function
The regular expression search () function