Regular Expressions in JavaScript-simple application

Source: Internet
Author: User

Regular Expressions in JavaScript-simple application

ECMAScript supports regular expressions through the RegExp type.

There are two ways to define a regular expression in JS:

1. Use constructors. Syntax: new RegExp (pattern [, flag]), where the first parameter pattern is a regular expression in string format, and the second parameter flag is an optional string.

// Match the first "bat" or "cat", Case Insensitive var pattern = new RegExp ("[bc] at", "I ");

2. Use words ------------
To define a regular expression. Syntax format: var reg =/pattern/[flag];

The preceding example can also be written as follows:

var pattern = /[bc]at/i

The regular expression matching mode supports the following three identifiers (flag ):

G: indicates the global mode, that is, the mode will be applied to all strings, rather than immediately stopping when the first match is found.

I: case-insensitive (case-insensitive) indicates that the case-insensitive mode and string are ignored when a matching item is determined.

M: indicates the multiline mode, that is, when the end of a line of text is reached, it will continue to find whether there is an item matching the mode in the next line.

 

Note that if you want to match some metacharacters in the mode, escape them. Escape metacharacters include: {, (, [, \, ^, $, | ,? , *,., +,],),}

 

For example, if you want to match [bc]

The literal mode can be written in this way.

var pattern = /\[bc\]at/i

The constructor must enclose the parameter strings in quotation marks. In some cases, double escape is required for the characters. Because the backslash itself needs to be escaped in quotation marks
var pattern = new RegExp("\\[bc\\]at","i");

We can see that the second method looks uncomfortable, so we usually define a regular expression literally.

Note that the regular expressions defined in these two methods are different. Although they are used to match strings, in ECMAScript3, the regular expression literally shares a RegExp instance, each new RegExp instance created using the constructor is a new instance.

 

Instance attributes of the regular expression object RegExp in JS:

1. global: Boolean value, indicating whether the g ID is set.

IgnoreCase: Boolean value, indicating whether the I ID is set.

Multiline: Boolean value, indicating whether the m id is set.

LastIndex: an integer that indicates the character position of the next matching item to be searched, starting from 0.

Source: String representation of a regular expression, which is returned literally.

 

RegExp instance method:

The common method in RegExp is test (), which accepts a string parameter. Returns true if the mode matches the modified parameter; otherwise, returns false. This method is convenient when you only want to know whether the target string matches a certain pattern and do not care about the content of the target string. It often appears in the if statement.

var test = "0000-1234567";var pattern = /\d{4}-\d{7}/;if(pattern.test(test)){    alert("match");}

A RegExp instance method is exec (), which is specially designed for grouping regular expressions. This method receives a string to apply the pattern as a parameter, returns an array containing the first matching item. In the array, the first item is the string that matches the entire pattern, and the other items are the strings that match the grouping in the pattern (if there is no grouping in the pattern, the array contains only one item ). For example:

Var text = "mom dad baby"; var pattern =/mom (dad (baby )?)? /Gi; // note the space var matches = pattern.exe c (text); alert (matches. index); // 0, index is an additional attribute of matches, indicating that the match is in the string position alert (matches. input); // The input attribute indicates the string that applies the regular expression. Here it is "mom dad baby" alert (matches [0]); // "mom dad baby" alert (matches [1]); // "dad baby" alert (matches [2]); // "baby"

Review:

We know that the String type also defines several methods for regular expression matching. One of them is the match () method. Calling this method on a string is essentially the same as calling the exec () method on a RegExp instance. Therefore, we can write the following in the preceding example:

var text = "mom dad baby";var pattern = /mom( dad( baby)?)?/gi;var matches = text.match(pattern);alert(matches.index);  //undefinedalert(matches.input);  //undefinedalert(matches[0]);  //"mom dad baby"alert(matches[1]);  //undefinedalert(matches[2]);  //undefined
After testing, Firefox and chrome I used didn't get the same effect, and I don't know what's going on, haha.

RegExp constructor attributes:

These attributes apply to all regular expressions in the scope and change based on the last regular expression operation executed. Here is an example to illustrate:

Var text = "she is a beautiful girl"; var pattern = /(.) eautiful/g; if (pattern. test (text) {alert (RegExp. input); // she is a beautiful girl, a string recently to be matched alert (RegExp. lastMatch); // beautiful, the latest matching item alert (RegExp. lastParen); // B, the last matched capture group alert (RegExp. leftContext); // she is a, the text alert (RegExp. multiline); // Boolean value, whether multiple alert (RegExp. rightContext); // girl, the text after lastMatch in the input 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.