JavaScript's RegExp Object

Source: Internet
Author: User
Tags character classes instance method

ECMAScript supports regular expressions through the REGEXP type. Using the following Perl -like syntax, you can create a positive
The expression

var expression =/pattern/flags;

The patternsection can be any simple or complex regular expression that can contain character classes, qualifiers, groupings,
Forward lookup and reverse reference. Each regular expression can have one or more flags (flags) that indicate the behavior of the regular expression.
The matching pattern for regular expressions supports the following 3 flags.

? g : Denotes global ( Global ) mode, that is, the pattern will be applied to all strings, rather than
stop immediately when the first occurrence is found;
? i : indicates case insensitive ( Case-insensitive ) mode, which ignores the case of patterns and strings when determining matches;
? m : denotes multiple lines ( Multiline
.

Example
/** matches all instances of "at" in the string */var pattern1 =/at/g;/** matches the first "bat" or "cat", case-insensitive */var pattern2 =/[bc]at/i;/** matches all 3-character combinations ending with "at" , case insensitive */var pattern3 =/.at/gi;

like regular expressions in other languages, all meta characters used in a pattern must be escaped. The metacharacters in a regular expression include:

( [ { \ ^ $ | ) ? * + .]}

These meta-characters regular have one or more special uses in the expression, so if you want to match the characters contained in the string,
They must be escaped. Here are a few examples.

/** matches the first "[Bc]at", Case-insensitive */var pattern2 =/\[bc\]at/i;/** matches all combinations of 3 characters ending with "at", without distinguishing between large */var pattern3 =/.at/gi;/** matching all ". At" , case insensitive */var pattern4 =/\.at/gi;

The preceding examples are all regular expressions defined in literal form. Another way to create a regular expression is to use the
The RegExp constructor, which receives two parameters: one is the string pattern to match and the other is an optional flag string. OK
Any expression defined with a literal can be defined using a constructor, as shown in the following example.

/** matches the first "bat" or "cat", case-insensitive */var pattern1 =/[bc]at/i;/** is the same as PATTERN1, except for */var pattern2 = new RegExp ("[Bc]at") created with the constructor , "I");

all metacharacters must be double escaped, that
The same is true for characters that have been escaped, for example \ n(character \ is usually escaped to \ \ In a string , and in a regular expression string
will become \\\\). The following table shows some patterns, the left is the literal form of these patterns, and the right is defined using the RegExp constructor
The same pattern as the string used.

Using regular expression literals is not the same as using regular expressions created with the RegExp constructor. In ECMAScript 3 ,
Regular expression literals always share the same RegExp instance, and every new REGEXP instance created with the constructor is

A new instance. Take a look at the following example.

varRe =NULL, I; for(i=0; I <Ten; i++) {Re=/cat/G;re.test ("Catastrophe");} for(i=0; I <Ten; i++) {Re=NewREGEXP ("Cat","g"); Re.test ("Catastrophe");}

In the first loop, even if it is specified in a loop body, it is actually only/cat/ Create a regexp instance. Because the real
Example property (the next section describes the instance properties) is not reset, calling test () call test () found "cat" 3 I can't find it. Because it will be tested to the end of the string, the next call to test () Second loop uses regexp constructors to create regular expressions in each loop. Because each iteration creates a new
regexp instance, each call to true /span>

instance properties of RegExp

Each instance of REGEXP has the following properties, which enable you to obtain various information about the pattern.

? Global: Boolean value that indicates whether the G flag is set .
? ignoreCase: Boolean value that indicates whether the I label is set
? lastIndex: Integer that starts the search for the next match
? Multiline: Boolean value that indicates whether the m flag is set
? Source: The string representation of a regular expression, literally
These properties allow you to learn about the aspects of a regular expression
In the schema declaration. For example:

varPATTERN1 =/\[bc\]at/I;alert (pattern1.Global);//falsealert (pattern1.ignorecase);//truealert (pattern1.multiline);//falsealert (Pattern1.lastindex);//0alert (Pattern1.source);//"\[bc\]at"varPATTERN2 =NewREGEXP ("\\[bc\\]at","I"); alert (pattern2.Global);//falsealert (pattern2.ignorecase);//truealert (pattern2.multiline);//falsealert (Pattern2.lastindex);//0alert (Pattern2.source);//"\[bc\]at"

RegExp instance method

JavaScript's RegExp Object

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.