Differences between the test, exec, and match methods in js Regular Expressions

Source: Internet
Author: User

This article mainly introduces the differences between the test, exec, and match methods in js regular expressions. If you need them, you can refer to them and hope to help you.

The differences between the test, exec, and match methods in the js regular expression indicate that test returns Boolean to find whether the corresponding string contains a pattern. Var str = "1a1b1c"; var reg = new RegExp ("1. "," "); alert (reg. test (str); // true exec find and return the current matching result, and return it as an array. Var str = "1a1b1c"; var reg = new RegExp ("1. "," "); var arr = reg.exe c (str); If the mode does not exist, arr is null; otherwise, arr is always an array with a length of 1, the value is the current match. Arr also has three attributes: the position of the current matching item of the index; the position of the end of the current matching item of the lastIndex (index + Length of the current matching item); input is str in the preceding example. The exec method is affected by the parameter g. If g is specified, the next time exec is called, it will start from the last matched lastIndex. Var str = "1a1b1c"; var reg = new RegExp ("1.", "" zookeeper alert(reg.exec(str;%0%%%%alert(reg.exe c (str) [0]); the above two outputs are 1a. Now let's take a look at the specified parameter g: var str = "1a1b1c"; var reg = new RegExp ("1. "," g "implements ;alert(reg.exec(str)%0%%%%%alert(reg.exe c (str) [0]); the first output is 1a, and the second output is 1b. Match is a method of String objects. Var str = "1a1b1c"; var reg = new RegExp ("1. "," "); alert (str. match (reg); match is a bit like exec, but exec is the RegExp object method; math is the String object method. There is another difference between the two, that is, the interpretation of the parameter g. If the parameter g is specified, match returns all results at a time. Var str = "1a1b1c"; var reg = new RegExp ("1. "," g "); alert (str. match (reg); // alert (str. match (reg); // the result of the preceding sentence is the same. This result is an array with three elements: 1a, 1b, and 1c. Regular Expressions are often used in JavaScript, while regular expressions often use the Match and Test functions, and Exec. here we use code instances to differentiate the differences between them. match Example code: var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var regexp =/[A-E]/gi; var rs = str. match (regexp); // rs = Array ('A', 'B', 'C', 'D', 'E', 'A', 'B ', 'C', 'D', 'E'); Test Example code: var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var regexp =/[A-E]/gi; var rs = regexp. te St (str); // rs = true; boolean Exc Example code: var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var regexp =/[A-E]/gi; var rs; while (rs = regexp.exe c (str ))! = Null) {document. write (rs); document. write (regexp. lastIndex); document. write ("<br/>");} OUTPUT---------------------------------A 1B 2C 3D 4E 5a 27b 28c 29d 30e 31 Another Exc Example code: var regexp =/AB */g; var str = "abbcdefabh"; var rs; while (rs = regexp.exe c (str ))! = Null) {document. write (rs); document. write (regexp. lastIndex); document. write ("<br/>");} OUTPUT ------------------------------- abb 3ab 9

Related Article

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.