Using the Exec () method in JS to construct regular expression verification _javascript techniques

Source: Internet
Author: User

Regular expressions, also known as formal representations, general representations. (English: Regular Expression, often abbreviated as regex, RegExp, or re) in code, a concept of computer science. A regular expression uses a single string to describe and match a sequence of rules that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.

Regular expressions in JavaScript

In JavaScript, you can use RegExp objects to construct regular expressions. We need to create a new instantiated regexp () object that can pass in two parameters: the first argument is a matching pattern, the second parameter is an optional, and three parameters can be passed in. I indicates a case-insensitive, G represents a global match, that is, matches all eligible strings, and m indicates that multiple matches are performed. Examples are as follows:

Copy Code code as follows:

var reg = new RegExp ("Hello", "I"); Represents the hello string in a matching string and is case-insensitive.

Second, the use of exec for pattern matching

There is a method in RegExp that can match the pattern and return the result: Exec (). This method is very important, basically is uses the JS to carry on the pattern matching necessary function. However, the return value of the function is not clear to many people, so it often goes wrong when it is actually used. Here is a systematic introduction to some of the exec () use methods.

The basic format for exec () is: Regexpobject.exec (String), where Regexpobject is a set up regular matching object, and string is the one to be matched. Returns an array if there is a successful match, or null if there is no successful matching part of the string.

The focus here is on this array. What exactly is the array returned? You can take a look at the following experiment.

Copy Code code as follows:

var re = new RegExp ("[? #&]" + user + "= ([^&#]*)", "I")

This code has a matching URL that can be used to get the parameters of the user=, so if you use a URL and use this mode for exec operations, what will the result return? For example, we have the following

www.qq.com?user=Tom&psw=123456

The result of the array returned by exec is: [? user=tom, Tom]. You can see that the first element that returns the array is the string that the entire matching pattern matches to, and the second matching character happens to be the parameter value.

This is the rule returned by the Exec match: The first element is the entire matching string, starting with the second argument to return the string that matches the grouping defined by each () in the pattern.

Here ([^&#]*) returns a string that does not begin with a & or #, that is, the corresponding argument.

If we modify the defined pattern to [? #&] "+ (user) +" = ([^&#]*), then the array returned after Exec () is [? user=tom, User, Tom].

The above is a small set to introduce the use of JS in the Exec () method to construct regular expression verification, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.