Regular expression foundation

xiaoxiao2021-03-06  58

Regular expression objects are used to specify a specification expression (that is, the expressioner does not conform to a specific requirement, such as the Email address format, etc.), which has the properties and methods for checking the given string .

In addition, the properties of individual regular expression objects established by the Regexp constructor have predefined the static properties of the regular expression object, you can use them at any time.

Core object:

Provided in JavaScript 1.2, NES 3.0 or later.

The Tosource method is added later in JavaScript 1.3.

Establishment method:

Text format or regexp constructor function.

Word establishment format uses the following format:

/ pattern / flags ie / mode / tag

The method of use of the constructor function method is as follows:

New regexp ("pattern" ["Flags"]), New Regexp ("Mode" [, "Tag"])

parameter:

Pattern (mode)

Text indicating regular expression

Flags (tag)

If this item is specified, the Flags can be one of the following values:

G: Global Match (full match)

I: Ignore Case (ignore the case)

GI: Both Global Match and Ignore Case (Matching all possible values, also ignore the case)

Note: Parameters in the text format do not use quotation markers, and parameters of the constructor use to use quotation marks. Therefore

Expression establishes the same regular expression:

/ ab c / i

New regexp ("ab c", "i")

description:

When using the constructor, you must use the normal string to avoid the rules (add preamble characters in the 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 complete pair of special characters that can be used in the regular expression.

Table 1.3: Special characters in regular expressions:

character/

Significance: For characters, it is usually represented by literal meaning, indicating that the following characters are special characters, / not explained.

For example: / b / match characters 'b', by adding a backslash /, that is, // b /, then the character becomes a special character, indicating

Match the boundary line of a word.

or:

For a few characters, it is usually noted that it is special, indicating that the following characters are not special, but should be explained by literally.

For example: * is a special character, matching any means (including 0 characters); for example: / a * / means matches 0 or more A.

In order to match the literal *, a backslash is added in front of a; for example: / a / * / match 'a *'.

Character ^

Significance: The character indicating that the match must be in the forefront.

For example: / ^ A / does not match 'A' in "AN A,", but matches the most in front of "AN A.".

Character $

Significance: Similar to ^, match the last character.

For example: / t $ / does not match 'T' in "Eater", but match 't' in "EAT".

character*

Significance: Match * The character is 0 times or N times.

For example: / bo * / match 'b' in 'boooo' or "A Bird Warbled" in "a ghost boooed", but does not match any of the characters in "a GoAT grunted".

Character

Significance: Match the characters in front of the number or N times. Equivalent to {1,}.

For example: / a / match All 'A' in 'A' in "Candy". "All 'A'.

character?

Significance: Match? The character is 0 times or 1 time.

For example: / e? Le? / Match 'el' and "Angle." In "Angel".

character.

Significance: (decimal point) matches all single characters except the linefill.

For example: /. N / match 'an' and 'on' in "NAY, An Apple IS on The Tree", but do not match 'NAY'.

Character (x)

Significance: Match 'X' and record the matching value.

For example: / (foo) / match and record "Foo Bar." In 'foo'. Matching substrings can be returned in the array of results [1], ..., [n]

Back, or returned by the property of the regexp object, ..., $ 9.

Character X | Y

Significance: Match 'x' or 'Y'.

For example: / Green | RED / Match 'Red' in 'Green' in "Green Apple".

Character {n}

Significance: The N here is a positive integer. Match the N characters in front.

For example: / a {2} / does not match 'a' in "Candy,", but matches all of 'A' and "Caaandy." In "Caandy,".

'a'.

Character {n,}

Significance: The N here is a positive integer. Match at least n front characters.

For example: / a {2,} does not match 'a' in "Candy", but matches all 'A' and "CaaaaaAAandy in" Caandy "all 'a'.

Character {n, m}

Significance: N and M here are integrated integers. Match at least n up to M phed characters.

For example: / a {1, 3} / does not match any of the characters in "cndy", but match "Caandy," in "Caandy" in "Caandy" in "Caandy"

The three 'A' in 'A' and "CaaaaaaAndy", pay attention: there are many 'A' in "caaaaaaandy", but only match the three

'A' "AAA".

Character [xyz]

Significance: A list of characters, matching any of the characters listed. You can use even characters - pointing out a range of characters.

For example: [ABCD] is the same as [A-C]. They match 'c' in 'B' and "Ache" in "Brisket".

Character [^ xyz]

Significance: A character is replenished, that is, it matches everything except the list listed. You can use a hobby - pointing out one

Character range.

For example: [^ ABC] and [^ a-C] equivalence, they first match 'h' in 'r' and "chop." In "BRISKET". Character [/ b]

Significance: Match a space (don't confuse with / b)

Character / B

Significance: Match the boundary line of a word, such as a space (don't confuse with [/ b])

For example: // BN / W / Matches 'NO', // WY / B / Match "Possibly YesterDay." In "Noonday" 'LY'.

Character / B

Significance: Match a non-bound line of a word

For example: // w / bn / match 'on', / y / b / w / match "Possibly YesterDay." In "Noonday" 'Ye'.

Character / CX

Significance: The x here is a control character. Match a control character of a string.

For example: // cm / matching Control-m in a string.

Character / D

Significance: Match a number, equivalent to [0-9].

For example: // d / or / [0-9] / match '2' in "B2 IS the suite number.".

Character / d

Significance: Match any non-figures, equivalent to [^ 0-9].

For example: // d / or / [^ 0-9] / match "B2 is the suite number." In 'b'.

Character / f

Significance: Match a form

Character / N

Significance: Match a newline

Character / R

Significance: Match a carriage

Character / s

Significance: Match a single White space character, including space, Tab, Form feed, wrap, equivalent, [/ f / n / r / t / v].

For example: // s / w * / match "foo bar." In 'bar'.

Character / s

Significance: Match a single character other than WHITE spaces, equivalent to [^ / f / N / R / T / V].

For example: // s // w * Match "Foo Bar." In 'foo'.

Character / T

Significance: Match a tab

Character / V

Significance: Matching a head tab

Character / W

Significance: Match all numbers and letters and underscore, equivalent to [A-ZA-Z0-9_].

For example: // W / Match '3' in 'A', "$ 5.28," in 'A', "$ 5.28," in '3' in "Apple," in "Apple,".

Character / W

Significance: Match other characters except numbers, alphabets, and underscore, equivalent to [^ A-ZA-Z0-9_].

For example: // w / or / [^ $ A-ZA-Z0-9 _] / match "50%." In '%'.

Character / N

Significance: The N here is a positive integer. Match the value of the N of the last substring of a regular expression (count left brackets).

For example: / apple, orange, below, "apple, orange, cherry, peach.", Below

There is a more complete example.

Note: If the number in the left spracted brackets is smaller than the number specified, the / n removes the octal ESCAPE of the row as a description.

Character / OOctal and / Xhex

Significance: The / Octal here is an octave Escape value, and / XHex is a hexadecimal ESCAPE value that allows an ASCII code to be embedded in a regular expression.

When the expression is checked, the text symbol provides a method of editing a regular expression. Use text symbols to make regular expressions

Maintains constants. For example, if you use a text symbol in a loop to construct a regular expression, regular expressions do not need

Repeated compilation.

Regular expression object constructors, for example, new regexp ("AB C"), providing runtime compilation of regular expressions. When you know

Then when the expression of the expression changes, the constructor should be used, or you don't know the pattern of the regular expression, and they are from additional

When the source is obtained, for example, when entering the user. Once you define a regular expression, the regular expression can be used anywhere,

And you can change, you can use the compilation method to compile a new regular expression for reuse.

A predefined regexp object can be used in each window; that is, each separated JavaScript thread is

Row to get your own regexp object. Because each script is uninterrupted in a thread, this ensures that different scripts will not be over.

Cover the value of the regexp object.

Predefined regexp objects include static properties: Input, Multiline, Lastmatch, Lastparen, LeftContext,

RightContext, as well as from $ 1 to $ 9. Input and Multiline properties are preset. The value of other static properties is to perform individual regular

After the EXEC and TEST methods of the expression object, and set after the Match and Replace methods of the string are executed.

Attributes

Note that several properties of the regexp object have both long names and short names (like Perl). These names are directed to the same value. Perl is

A programming language, while JavaScript imitates its regular expression.

Property $ 1, ..., $ 9

Get the matching substrings, if any

Property $ _

Refer to INPUT

Property $ *

Reference multiline

Properties $ &

Refer to Lastmatch

Attribute $

Refer to Lastparen

Property $ `

Reference LeftContext

Attribute $ '

Reference Rightcontext

Property Constructor

Specifies to establish an object prototype letter

Property Global

Decide whether the test regular expression cannot match all strings, or just conflict with the first one.

Attribute ignorecase

Decide whether to ignore the case when trying to match the string

Property Input

When the regular expression is matched, for the opposite string.

Property LastIndex

Decided that the next match starts from there.

Attribute Lastmatch

The last matching character

Attribute Lastparen

When the substring is matched, the last Parenthesized, if any.

Attribute LeftContext

Recently, the child string is matched.

Property Multiline

Do you search in multiple rows of strings.

Property prototype

Allow additional properties to all objects

Property Rightcontext

Recently matched subsiders.

Property Source

Pattern text

method

Compile method

Compile a regular expression object

EXEC method

Run regular expression match

Test method

Test regular match

Tosource method

Returns a text description specified by an object; you can use this value to create a new object. Don't consider Object.tos

Ource method.

Tostring method

Returns a string describing the specified object, regardless of the object.tostring object.

Valueof method

Returns the original value of the specified diagonal. Object.valueof methods are not considered.

转载请注明原文地址:https://www.9cbs.com/read-116991.html

New Post(0)