JS--REGEXP type

Source: Internet
Author: User

ECMAScript supports regular expressions through the regexp type.

Grammar:

var expression =/pattern/flags;

The matching pattern for regular expressions supports the following 3 flags.

1.G: Represents the global pattern, which means that the pattern will be applied to all strings, rather than stopping immediately when the first occurrence is found.

2.I: Indicates case-insensitive mode, which ignores pattern and string case when determining matches.

3.M: Represents a multiline pattern, that is, when the end of a line of text is reached, it also continues to find out if the next line exists in the pattern-matching item.

There are two ways to create a regular expression:

1. Literal form

For example:

var pattern =/at/g;

2. Using the RegExp constructor

For example:

var New RegExp ("[Bc]at", "G");

Note: The two arguments passed to the RegExp constructor are strings and cannot pass the regular expression literal to the constructor, so in some cases double-escaping the character. All meta characters are double-escaped.

For example:

/\[bc\]at/"                    \\[bc\\]at"/\w\\hello\\123/          "\\w\\\\hello\\\\123"  

Note: Regular expressions created using regular expression literals and regexp constructors are not the same. In ECMASCRIPT3, regular expression literals always share a single regexp instance, and each new RegExp instance created by the RegExp constructor is a new instance. ECMAscript5 explicitly stipulates that the use of regular expression literals must create a new instance every time, just as the RegExp constructor is called directly.

RegExp Instance Properties:

1.global: Boolean value that indicates whether the G flag is set.

2.ignoreCase: Boolean value that indicates whether the I flag is set.

3.lastIndex: Integer that represents the character position of the search for the next occurrence, starting from 0.

4.multiline: Boolean value that indicates whether the M flag is set.

5.source: The string representation of a regular expression, returned in literal form rather than in the string pattern in the incoming constructor.

RegExp instance method:

1.exec (), which is specifically designed for capturing groups. EXEC () takes a parameter that applies both the pattern string and the array that contains the first match information, or returns NULL if there is no match. The returned array, although an instance of array, contains two additional attributes: Index and input. Where index represents the position of the match in the string, and input represents the string to which the regular expression is applied. In the array, the first item is a string that matches the entire pattern, and the other item is a string that matches the capturing group in the pattern (if there is no capturing group, the array contains only one item).

For example:

 var  text = "Mom and Dad and baby"  var  pattern =/mom (and dad (and baby)?) /GI;  var  matches = pattern.exec (text); alert (        Matches.index);         // 0  alert (matches.input); //             "Mom and dad and Baby"  alert (matches[0]); //             "Mom and dad and Baby"  alert (matches[1]); //             "and dad and Baby"  alert (matches[2]); //  "and baby"  

Note: Setting the G flag or not will affect the number of matches searched, and the Lastindex property.

2.test (), which accepts a string parameter. Returns true if the pattern matches the parameter, otherwise returns FALSE.

A String object method that supports regular expressions:

Search ()

Match ()

Replace ()

Split ()

JS--REGEXP type

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.