The $1-$9 of the static properties of the JavaScript regular expression regexp

Source: Internet
Author: User

JavaScript Regular Expressions support packet capture, and the results of grouping are stored in the static property $1-$9 of RegExp. That is, when the number of groups in a regular expression exceeds 9, the regexp retains a maximum of 9.

Regular Var with 10 groupings RegExp =/(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)/;var str = ' A-b-c-d-e-f-g-h-i-j-k-l-m-n-o ';//regexp.test (str); regexp.exec (str); for (Var i=1;i<=10;i++) {Console.log (regexp ["$" +i]);}

1.The $, ..., $9 properties is static, there is not a property of an individual regular expression object. Instead, them as regexp.$1, ..., regexp.$9.

2.The values of these properties are read-only and modified whenever successful matches is made.

var re =/(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)-(\w)/;var str1 = ' a-b-c-d-e-f-g-h-i-j '; var str2 = ' Z-y-x-t-s-r-q-p-o-n '; re.test (str1); for (Var i=1;i<=10;i++) {Console.log (regexp["$" +i]);} Match failed, $1-$9 invariant re.test ("A-b-c"), for (Var i=1;i<=10;i++) {Console.log (regexp["$" +i]);} Match succeeded, $1-$9 change re.test (STR2); for (Var i=1;i<=10;i++) {Console.log (regexp["$" +i]);}


can refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n





The $1-$9 of the static properties of the JavaScript regular expression regexp

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.