Regular expression regular expression details

xiaoxiao2021-03-06  98

Regular expression is regular expression, it seems that English is better than Chinese, that is, check the expression character does not meet the requirements! ! Regular expressions have a function of very powerful and complex object regexp, provided above JavaScript1.2. Let's take a look at the introduction of regular expressions: Regular expression objects are used to specify a specification expression (that is, the expressioner does not conform to specific requirements, such as the email address format, etc.), it has it to check Whether the string is in line with the properties and methods of the rules. 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. Establish a method: text format or regexp constructor function. Word establishment format uses the following format: / pattern / flags ie / mode / tag

The constructor function method is used as follows: New regexp ("pattern" ["Flags"]), NEW Regexp ("Mode" [, "Mark"])

Parameters: Pattern Represents the text of the regular expression

Flags If you specify this, Flags can be one of the following values: G: Global Match i: Ignore Case (ignore the case) GI: Both Global Match and Ignore Case (match all possible values, Also ignore cases)

Note: Parameters in the text format do not use quotation markers, and parameters of the constructor use to use quotation marks. So the following 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 two statements below 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 turns into a special character, indicating the boundary line of the matching 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 literal. 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 matching character must be at 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 'Boooo' or "A Bird Warbled" in "a ghost booooed", but does not match any of the "a Goat G Runted".

Character Significance: Match the character in front of the number once or N times. Equivalent to {1,}. For example: / a / match All 'A' in 'A' in "Candy". "All 'A' in" CaaaaAAAandy. ".

Character? Significance: Match? The character is 0 times or 1 time. For example: / e? Le? / Match 'el' and "Angle." In "Angel".

Character. Significance: (decis) matches all single characters except the linefinder. 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 logs the matching value. For example: / (foo) / match and record "Foo Bar." In 'foo'. Matching substrings can be returned by the group in the result array [1], ..., [n], or returned by the property of the regexp object, ..., $ 9.

Character X | Y Sign: Match 'x' or 'Y'. For example: / Green | RED / Match 'Red' in 'Green' in "Green Apple".

Character {n} meaning: 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". ".

Character {n,} Significance: The N 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 positive integers. Match at least n up to M phed characters. For example: / a {1, 3} / does not match any character in "cndy", but matches "Caandy," in "Caandy," in front of "Caandy," in front of "CaaaaaaaAandy" in front Three 'a', pay attention: There are many 'a' in "CaaaaaAndy", but only match the three 'A' "AAA" in front.

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 listed characters. You can use a hyphen - pointing out a range of characters. 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 a word line, such as a space (do not confuse with [/ b]), for example: // BN / W / Match "noonday" in 'NO', // WY / B / Match " "ly 'in POSSIBLY YESTERDAY."

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".

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 Enterprise

Character / s Significance: Match a single White space character, including space, Tab, Form feed, wrap, equivalent to [/ 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 (,) / sorange / 1 / Match "Apple, Orange, Cherry, Peach." The following has 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 / OOctal 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. Using the text symbols can keep the regular expression to remain constant. For example, if you use a text symbol in a loop to construct a regular expression, the regular expression does not need to be compiled repeatedly. Regular expression object constructors, for example, new regexp ("AB C"), providing runtime compilation of regular expressions. When you know that the regular expression is changed, you should use the constructor, or you don't know the mode of the regular expression, and they are obtained from additional sources, such as input by the user. Once you define a regular expression, the regular expression can be used anywhere and can be changed, you can use the compilation method to compile a new regular expression for re-use. A pre-defined regexp object can be used in each window; that is, each separate JavaScript thread is run to get its own regexp object. Because each script is uninterrupted in a thread, this ensures that different scripts do not cover the value of the Regexp object. Predefined regexp objects include static properties: Input, Multiline, Lastmatch, LastParen, LeftContext, RightContext, and from $ 1 to $ 9. Input and Multiline properties are preset. The value of other static attributes is after executing the EXEC and TEST methods of individual regular expression objects, and after performing the Match and Replace methods of the string and the replace method.

Attributes Note 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.

Attribute $ 1, ..., $ 9 acquires matching substrings, if any

Property $ _ Reference INPUT

Property $ * Reference Multiline

Properties $ & Refer to Lastmatch

Property $ Reference Lastparen

Property $ `Reference LeftContext

Property $ 'Reference Rightcontext

Attribute constructor specifies to establish an object prototype letter

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

Attribute IgnoreCase decides whether to ignore the case when trying to match the string

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

Attribute LastIndex decision next match starts from there

Property LastMatch Last Match Character

Attribute LastParen substring matches, the last parenthesized, if any.

Attribute LeftContext has recently matched a substring before matching.

The property Multiline is searching in multiple rows of strings.

Properties prototype allows additional properties to all objects

Attribute RightContext has recently matched a substring.

Property Source Mode Text

Method Compile method compiles a regular expression object

EXEC method runs regular expression match

TEST method test regular match

The TiSource method returns a text description of an object specified; you can use this value to create a new object. The Object.tos Ource method is not considered. The toString method returns a string describing the specified object, regardless of the object.toString object.

The Valueof method returns the original value of the specified diagonal. Object.valueof methods are not considered.

In addition, this object inherits the Watch and Unwatch methods of the object.

Example: Example 1, the following example script uses the Replace method to convert the words in the string. In the replaceable text, the script uses the value of $ 1 and $ 2 attributes of the global REGEXP object. Note that when it is passed to the Replace method as the second parameter, the name of the $ attribute of the regexp object is called.