Function: the value of index is the same as that returned by search. Whether or not appropriate content is matched, if appropriate content is found, index indicates the starting position of the actual character of the matched content. If no content is found,-1 is returned.
Basic Syntax: RegExp. index
Note: This property is also a static property of RegExp, and the call method is fixed.
Copy codeThe Code is as follows:
<Html>
<Script language = "javascript" type = "text/javascript">
// Alert ('designed By Androidyue ');
// Set a regular expression that matches numbers similar to 12021, with the same single and tens of thousands of digits and only one match outside the world.
Var re =/(/d)/d/2/1 /;
// Var re =/3 (/d + )/;
// Data to be matched
Var objStr = '20140901 ';
// Use the search method to return to Shanghai and match the start position of the content for the first time. Note that the initial address of the string is 0, not 1.
Var pos = objStr. search (re );
// If no matching content is found in the search method, 1 is returned.
If (pos =-1 ){
// Document. write ("Matching content not found! "+ RegExp. index );
Document. write ("Matching content not found! ");
} Else {// if the search method can find the appropriate Matching content, return the starting position of the matching content string.
Var arr = objStr. match (re); // Save the Matching content to the array.
Document. write ("the first match is found at the" + pos + "position, and the matched content is" + arr [0] + "<br> ");
Document. write ("RegExp. the index value is "+ RegExp. index); // display 1 (the same as pos) in the ie core browser. In chrome, ff, and opera, undefined is displayed.
}
</Script>
</Html>