Explore JS Regular match method: Match and exec

Source: Internet
Author: User

Match is a string method, written as: Str.match (REG)

exec is a regular expression method, written as: Reg.exec (str)

Match and Exec return an array when the match succeeds, and null when no match is made, so it would be a mistake to use the same effect if the two rules are not well understood, and in several cases, match and exec are distinguished.

1. Global match:

  When a global match is not used, the matching effect is the same, returning only the results of the first successful match:

var s = "AAA bbb CCC"; var reg =/\b\w+\b/; // No G var rs_match = s.match (reg); var rs_exec = reg.exec (s); Console.log ("match:", Rs_match); Console.log ("exec:", Rs_ EXEC);

When a global match is used, the matching result differs between the two:

var s = "AAA bbb CCC"; var reg =/\b\w+\b/g; // have g var rs_match1 = s.match (reg); var rs_match2 = s.match (reg); var rs_exec1 = reg.exec (s); var rs_exec2 = reg.exec (s), Console.log ("match1:", Rs_match1); Console.log ("MATCH2: ", Rs_match1); Console.log (" EXEC1: ", RS_EXEC1); Console.log (" EXEC2: ", RS_EXEC2);

A, when global matches, match returns all matches, while exec matches only the content on a single match

B, global match and multiple matches, EXE will start matching from the next one at the end of the last match, return the content on this match until nothing can match, return null

  

2. Group:

When there are no global matching groupings, match and exec return the same results. Since the regular expression is grouped in parentheses, all the groupings of the result are returned at the same time that the matching result is returned:

var s = "Aaa1 bbb2 ccc3"; var reg =/\b (\w+) (\d{1}) \b/; // two groups, no G var rs_match1 = s.match (reg); var rs_match2 = s.match (reg); var rs_exec1 = reg.exec (s); var rs_exec2 = reg.exec (s), Console.log ("match1:", Rs_match1); Console.log ("MATCH2: ", Rs_match1); Console.log (" EXEC1: ", RS_EXEC1); Console.log (" EXEC2: ", RS_EXEC2);

When a global match is grouped, match and exec return different results. Match returns all matching results, and exec returns the result of the match, and if a grouping occurs in the expression, all the groupings of this match are returned in turn:

vars = "Aaa1 bbb2 ccc3";varReg =/\b (\w+) (\d{1}) \b/G;varRs_match1 =S.match (reg);varRS_MATCH2 =S.match (reg);varRS_EXEC1 =Reg.exec (s);varRS_EXEC2 =Reg.exec (s);varRS_EXEC3 =Reg.exec (s);varRS_EXEC4 =Reg.exec (s); Console.log ("Match1:", Rs_match1); Console.log ("MATCH2:", Rs_match1); Console.log ("EXEC1:", RS_EXEC1); Console.log ("EXEC2:", RS_EXEC2); Console.log ("EXEC3:", RS_EXEC3); Console.log ("EXEC4:", RS_EXEC4);

Explore JS Regular match method: Match and exec

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.