Index attributes (RegExp object) of js Regular Expressions)

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.