Regular Expressions in JS

Source: Internet
Author: User
Tags alphabetic character character classes

A regular expression is an object that describes a character pattern, whose main function is to pattern-match and text-retrieve a string, and then to match the string (which may be empty) according to the contents of the regular description.
Table retrieval success, based on the result string can be another step of the text substitution, and other functions, the match is unsuccessful in what results are not. Pattern matching of regular expressions always looks for the first possible match in a string
The position is that it always matches from left to right.

The essence and creation of the regular expression

1, the regular expression is regexp is a reference type in JS, can be instantiated to get an actionable object.

2. How to create:

Regular literal form var pattern =/bfy/gi,es3 Specifies that the same instance object is returned for each run of the same literal initialization, which is unreasonable, ES5 corrects this place, returns new instances more in line with the developer's expectations, or can
To reduce the "residue" of the lastindex value of the program caused by unintended effects;

The constructor form var pattern = new RegExp ("Bfy", "GI"), the string parameter in the constructor form needs to be double-escaped (meta-character and escape character).

3. The RegExp type has the following static properties

Input, Lastmatch, Lastparen, Leftcontext, Multiline, Rightcontext. These properties have a corresponding short property name, where three can be used as a string as the second parameter of the replace () method, and there are $1-$9 storage capturing groups.

The static properties of the regexp dynamically depend on a pattern match that occurred recently.

Two, the composition of the regular expression

A regular expression can be seen as a combination of a series of characters.

The characters in 1,///are pattern-matched, mainly with direct volume characters, escape characters, metacharacters (anchor characters and special grammatical characters), and character classes.

Direct volume characters are most common, and are matched one-to-a-bfy;

The escape character supports non-alphabetic character matching, such as \ n matches newline characters, and special escape characters such as \w represents a collection of numbers and letters;

The main function of metacharacters is to describe the relevant syntax, such as the anchor character to specify the matching position (^ match start, $ match end), and special syntax characters (| to indicate selection mode);

The character class represents a class of characters that can be used for matching, divided into ordinary character classes [...] and the negative character class [^ ...].

2,//Outside of the character is called modifier, the regular pattern matching control, there are I, M, G.

Third, the special syntax of regular expressions

It is not only possible to make simple character-to-character matching, but there are several special grammars that can be used.

1. Repeat ——— to match repeated characters

Repeating characters: {} 、?、 +, *. Repeating the syntax by default is greedy mode that is, the regular will try to match more characters, if you do not want such a pattern, you can start non-greedy mode is followed by a repeating character?.

2. Select ——— specify multiple pending matches in a regular

Select the character: |, match from left to right, match to end.

3, grouping and its references--grouping pairs of expressions

Grouping characters: ()--the grouping that is captured by default, (? :)--a grouping that will not be captured

The role of Grouping: (1) after the generation of sub-expressions can be treated as a whole, such as adding repetition, making choices and so on, (2) extracting sub-pattern, that is, can capture the sub-expression matched to the part of the string, with the \ number to refer to.

Iv. attributes of a regular object

1. A string representation of the literal form of the source--regular expression;

2, global--whether to open the global;

3, ignorecase--whether to open the screen case;

4, multiline--whether to open multi-line matching mode;

5, lastindex--the next time the matching behavior begins to retrieve the location, the next time the string does not match to make a second match before it is 0 to recharge it, otherwise it will be left the previous index.

V. Using regular expressions for pattern matching in scripts

JS provides two ways of using regular expressions for pattern matching.

1. String calls related methods for pattern matching

(1) search ()-Strict pattern matching method, parameters are cast to regular objects. Global g is not supported, returning the starting position of the matching string or-1;

(2) Replace ()--not strict pattern matching method, the first parameter can be a string or a regular, the second argument can be a string or a function (combined with the Swich structure can be very flexible operation), there is no actionable return value;

(3) match ()-strict pattern matching method, parameters are cast to regular objects. Returns an array of all matched strings in the global mode, returns a non-global pattern for the information about the first match behavior, and fails returns null;

(4) Split ()--non-strict pattern matching method, the first parameter is a string or regular, the second argument is optional, specify the number of elements into the result array, return the result array.

2. Regular object calls related methods for pattern matching

(1) EXEC ()-similar to match (), only returns an array of information about the matching behavior, whether or not the global schema is returned;

(2) test ()--only focus on whether the match, does not care about matching the result string, returns a Boolean value.

Original address: www.bfy0412.com

Regular Expressions in JS

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.