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= "<">"; - Test (str); +</script> -</body> +Test environment: Version 40.0.2214.115 m
Test results:
- A represents the complete substring of the match, such as:<
- 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:<">
- 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