javascript--parameters of a function function in the Replace function of a string

Source: Internet
Author: User

1<! Doctype>2345<body>6<script type= "Text/javascript" >7             functionTest (s) {8                 varpattern=/& ([^&;] +);/G;9S.replace (Pattern,function(a,b,c,d,e) {Ten alert (a); One alert (b); A alert (c); - alert (d); - alert (e); the                 }); -             } -             varStr= "&lt;&quot;&gt;"; - Test (str); +</script> -</body> +

Test environment: Version 40.0.2214.115 m

Test results:

    • A represents the complete substring of the match, such as:&lt;
    • b indicates the successful part of the match, such as: LT
    • c indicates the subscript of the matched complete substring, such as: 0
    • D indicates a test string, such as:&lt;&quot;&gt;
    • E for undefined

About "capturing group" in regular expressions, refers to the regular expression through () the matching pattern is divided into groups, you can get the first capturing group through regexp.$1, Regexp.$2 get the second capturing group, such as:

1<! Doctype>2345<body>6<script type= "Text/javascript" >7             vartext= "111-22-3333";8             varpattern=/(\d{2,})-(\d{2,})-(\d{2,})/G;9             if(pattern.test (text)) {TenAlert (regexp.$1); OneAlert (regexp.$2); AAlert (regexp.$3); -             }         -</script> the</body> -

Output "111", "22", "3333"

javascript--parameters for function functions in the Replace function of a string

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.