Regular expressions in JavaScript

Source: Internet
Author: User
Tags character classes

ECMAScript supports regular expressions through the regexp type.

  The creation of regular expressions

Using Perl-like syntax, you can create a regular expression.

var expression =/pattern/flags;

The pattern section can be any simple or complex regular expression that can contain character classes, qualifiers, groupings, forward lookups, and reverse references. 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: Represents the global mode, which means that the pattern will be applied to all strings, rather than stopping 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: represents multi-line (multiline) mode, that is, when you reach the end of a line of text, you also continue to find out if there are items in the next row that match the pattern.

Therefore, a regular expression is a combination of a pattern with the above 3 flags. Different combinations produce different results, as follows:

/**/var pattern1 =/at/g; /*  */var pattern2 =/[bc]at/i; /*  */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-character regular have one or more special uses in the expression, so if you want to match the characters contained in the string, you must escape them.

/**/var pattern2 =/[bc]at/i; /*  */var pattern2 =/\[bc\]at/i; /*  */var pattern3 =/.at/gi; /*  */var pattern4 =/\.at/gi;

Another way to create a regular expression is to use the RegExp constructor, which receives two parameters: one for the string pattern to match, and the other as an optional flag string. Any expression that can be defined with a literal can be defined using a constructor.

/**/var pattern1 =/[bc]at/i; /*  */varnew RegExp ("[Bc]at", "I");

Note that the two arguments passed to the RegExp constructor are strings. Because the schema parameter of the RegExp constructor is a string, in some cases you want to double-escape the string. All metacharacters must be double-escaped, as are the characters that have escaped. For example \ n (character \ is usually escaped to \ \ In a string and becomes \\\\ in the regular expression string). The following table shows some patterns, the left is the literal form of these patterns, and the right is the string used when defining the same pattern using the RegExp constructor.

Literal mode An equivalent string
\[bc\]at \\[bc\\]at
\.at \\.at
Name\/age Name\\/age
\d.\d{1,2} \\d.\\d{1,2}
\w\\hello\\123 \\w\\\\hello\\\\123

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 each new RegExp instance created with the constructor is a new instance. However, it is explicitly stated in ECMAScript 5 that the use of regular expression literals must create a new RegExp instance every time, just as the RegExp constructor is called directly. ie9+, Firefox 4+ and chrome have been modified accordingly.

  RegExp Instance Properties

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 flag is set.

? LastIndex: An integer that represents the character position at which to start searching for the next occurrence, starting from 0.

? Multiline: Boolean value that indicates whether the M flag is set.

? Source: A string representation of a regular expression that is returned in literal form rather than in the string pattern in the incoming constructor.

Not to be continued ...

Regular expressions in JavaScript

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.