Regular expression Regular expression detailed (i)

Source: Internet
Author: User
Tags format constructor integer range regular expression
express| regular Regular expression is regular expression, it seems that English is better than Chinese understand more, is to check expression characters
Not meet the rules!! Regular expressions have a very powerful and very complex object regexp, in the JavaScript1.2 version to
On offer.
Let's look at an introduction to regular expressions:
Regular expression objects are used to standardize an expression of a specification (that is, an expression character that does not meet specific requirements, such as whether it is an email
Address format, and so on, which has properties and methods used to check whether the given string conforms to the rules.
In addition, you have predefined regular expressions by using the properties of individual regular expression objects created by the RegExp constructor.
objects, you can use them at any time.
Core objects:
In JavaScript 1.2, NES more than 3.0 versions are available.
After the JavaScript 1.3 version added the Tosource method.
To establish a method:
Text formatting or regexp constructor functions.
Text formatting uses the following format:
/pattern/flags/Mode/tag

The constructor function method uses the following methods:
New RegExp ("pattern" [, "Flags"]) is new REGEXP ("mode" [, "tag"])

Parameters:
Pattern (Mode)
Text that represents the regular expression

Flags (tags)
If you specify this item, flags can be one of the following denominations:
G:global match (full set matching)
I:ignore case (ignoring capitalization)
Gi:both Global Match and ignore case (matches all possible values, also ignores capitalization)

Note: The parameters in the text format do not use quotation marks, and constructor function parameters are marked with quotation marks. So the following
Expression to create the same regular expression:
/ab+c/i
New RegExp ("Ab+c", "I")

Describe:
When using constructors, it is necessary to use normal string avoidance rules (adding leading characters in a string).
For example, the following two statements are equivalent:
Re = new RegExp ("\\w+")
Re =/\w+/

The following provides a complete list and description of the special characters that can be used in regular expressions.

Table 1.3: Special characters in regular expressions:

Characters
Meaning: For a character, it is usually a literal meaning that the character followed by a special character is not interpreted.
For example:/b/matches the character ' B ', which becomes a special character by adding a backslash, or/\b/, in front of B, which means
Match the dividing line of a word.
Or:
For a few characters, usually the description is special, indicating that the character immediately followed is not special, but should be interpreted literally.
For example: * is a special character that matches any character (including 0 characters), for example:/a*/means matching 0 or more aces.
To match the literal *, precede a with a backslash; For example:/a\*/matches a ' * '.

Character ^
Meaning: The character that matches must be at the front.
For example:/^a/does not match the ' a ' in ' an A ', but matches ' an A. ' The "A" in the front.

Character $
Meaning: Similar to ^, matches the last character.
For example:/t$/does not match ' t ' in ' eater ', but matches ' t ' in ' eat '.

Characters
Meaning: matches the characters of the preceding 0 or n times.
For example:/bo*/matches ' B ' in ' boooo ' or ' a bird warbled ' in ' a ghost booooed ', but does not match ' a goat g
runted any character in the ".

Character +
Meaning: Matches the character preceded by the + number 1 or n times. Equivalent to {1,}.
For example:/a+/matches ' a ' and ' Caaaaaaandy ' in ' Candy '. All ' a ' in.

Character?
Meaning: match the preceding character 0 or 1 times.
For example:/e?le?/matches ' el ' and ' angle ' in ' Angel '. ' Le ' in the.

Character.
Meaning: (decimal point) matches all individual characters except for line breaks.
For example:/.n/matches ' an ' and ' on ' in the ' nay ', an apple are on the tree, but does not match ' nay '.


Character (x)
Meaning: Matches ' X ' and records the matching value.
For example:/(foo)/Match and record "Foo bar." In the ' foo '. The matching substring can be in the result array of the element [1], ..., [n] Return
Back, or the attributes of the RegExp object are $, ..., the $ return.

Character X|y
Meaning: Match ' x ' or ' Y '.
For example:/green|red/matches ' green ' and ' Red apple ' in ' green apple '. In the ' Red '.

Character {n}
Meaning: Here The n is a positive integer. Matches the preceding n characters.
For example:/a{2}/does not match "a" in "Candy," but matches "Caandy," all ' a ' and "Caaandy." Two in front of
' A '.

Character {n,}
Meaning: Here The n is a positive integer. Matches at least n preceding characters.
For example:/a{2,} does not match ' a ' in ' Candy ', but matches all ' a ' and ' Caaaaaaandy ' in ' Caandy '. All ' a ' in the

Character {n,m}
Meaning: Here the N and M are all positive integers. Matches at least n up to m preceding characters.
For example:/a{1,3}/does not match any of the characters in "Cndy", but matches the first two in "a", "Caandy," in "Candy,"
"A" and "Caaaaaaandy" in front of the three ' a ', note: even if "caaaaaaandy" there are many ' a ', but only match the previous three
A ' a ' or ' AAA '.

character [XYZ]
Meaning: A list of characters that matches any one of the characters listed. You can indicate a range of characters through hyphens.
For example: [ABCD] is the same as [a-c]. They match the ' C ' in ' B ' and ' ache ' in ' brisket '.

character [^XYZ]
Meaning: A character complement, that is, it matches everything except the characters listed. You can use hyphens--point out a
The range of characters.
For example: [^ABC] and [^a-c] are equivalent, they first match the ' R ' and ' chop ' in ' brisket '. In the ' H '.

character [\b]
Meaning: Match a space (do not confuse with \b)

Character \b
Meaning: A line that matches a word, such as a space (not confused with [\b])
For example:/\bn\w/matches ' no ' in ' Noonday ',/\wy\b/matches ' possibly yesterday. ' In the ' ly '.

Character \b
Meaning: A non-dividing line that matches a word
For example:/\w\bn/matches ' on ' in ' Noonday ',/y\b\w/matches ' possibly yesterday. ' In the ' ye '.

Character \cx
Meaning: Here The X is a control character. Matches the control character of a string.
For example:/\cm/matches the control-m in a string.

Character \d
Meaning: Matches a number, equivalent to [0-9].
For example:/\d/or/[0-9]/matches "B2 is the" suite number. In the ' 2 '.

Character \d
Meaning: Matches any non-numeric, equivalent to [^0-9].
For example:/\d/or/[^0-9]/matches "B2 is the" suite number. In the ' B '.

Character \f
Meaning: matches a form character

Characters \ n
Meaning: matches a newline character

Character \ r
Meaning: matches a carriage return character

Character \s
Meaning: Matches a single white spaces, including spaces, tab,form feeds, line breaks, equivalent to [\f\n\r\t\v].
For example:/\s\w*/matches "foo bar." ' Bar ' in the.

Character \s
Meaning: matches a single character except white spaces, equivalent to [^ \f\n\r\t\v].
For example:/\s/\w* matches "foo bar." In the ' foo '.

Character \ t
Meaning: Match a tab

Character \v
Meaning: Match a head tab

Character \w
Meaning: Matches all numbers and letters and underscores, equivalent to [a-za-z0-9_].
For example:/\w/matches ' 5 ' and ' 3D ' in ' a ', ' $5.28, ' in ' Apple, '. In the ' 3 '.

Character \w
Meaning: Matches other characters except numbers, letters, and underscores, equivalent to [^a-za-z0-9_].
For example:/\w/or/[^ $A-za-z0-9_]/match "50%." In the '% '.

Characters \ n
Meaning: Here The n is a positive integer. Matches the value of N of the last substring of a regular expression (counting the left parenthesis).

For example:/apple (,) \sorange\1/match "Apple, orange, cherry, peach." In the ' Apple, Orange ', below
There is a more complete example.
Note: If the number in the left parenthesis is smaller than the number specified in \ n, then \ n takes a row of octal escape as a description.

Character \ooctal and \xhex
Meaning: Here the \ooctal is a octal escape value, and \xhex is a hexadecimal escape value that allows for a
Embed the ASCII code in the regular expression.


When an expression is checked, a literal symbol provides a way to edit the regular expression. Using literal symbols to make regular representations
The type remains constant. For example, if you use a literal symbol to construct a regular expression in a loop, the regular expression does not need to be
Compile repeatedly.
The regular Expression object builder, for example, new RegExp ("Ab+c"), provides run-time compilation of regular expressions. When you know that you are
When the pattern of expressions changes, you should use constructors, or you don't know the pattern of regular expressions, and they are from the other
When the source is obtained, such as when entered by the user. Once you have defined the regular expression, the regular expression can be used anywhere,
And can be changed, you can use the compile method to compile a new regular expression for reuse.
A separate predefined RegExp object can be used in each window, that is, each detached JavaScript line Cheng
Line to get your own RegExp object. Because each script is not interrupted in one thread, this ensures that different scripts do not override
The value of the cover RegExp object.
The predefined RegExp objects contain static properties: Input, Multiline, Lastmatch,lastparen, Leftcontext,
Rightcontext, and from $ to $. Input and Multiline properties are preset. The values of other static properties are in the execution of individual regular
After the exec and test methods of an expression object, and after the match and replace methods of the string are executed.

Property
Note that several properties of the RegExp object have both long and short names (like Perl). These names all point to the same value. Perl is
A programming language, while JavaScript imitates its regular expression.

Properties $, ..., $
Gets a matching substring, if any.



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.