Regular expression regular expression detailed in Article Author: Anonymous Published: 2004-6-3 10:05:02
Regular expression regular expression Details (1)
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 very powerful and very complex object regexp, provided above the 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 Use the following format: / pattern / flags ie / mode / Mark constructor function method Usage: New regexp ("pattern" [, "flags"]), New Regexp ("Mode" [, "Tag"] Parameters: Pattern Represents the text Flags (tag) of the regular expression, if this item is specified, the Flags can be one of the following values: G: Global Match (full match) i: ignore case GI: Both Global Match and Ignore Case (Matching all possible values) Note: Parameters in the text format do not use quotation marks, and the parameters of the constructor function 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 a leading character in a string) is necessary. For example, the following two statements are equivalent: RE = New regexp ("/ w ") RE = / W / below provides a complete list and description of a complete pair of special characters that can be used in regular expressions . Table 1.3: Special characters in the regular expression: Character meaning: For characters, it is usually represented by literal meaning, indicating that the characters are special characters, not explained. For example: / b / match character 'b', by adding a backslash in front of B, that is, / b /, then the character turns into a special character, indicating the boundary 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 '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 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'. 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' characters in "Caandy" all 'A' characters {n, m} meaning: here N and M are all 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 (do not confuse with B) Character B Significance: Match a word line, such as a space (do not confuse with [b]), for example: / bnw / match "noonday" in 'NO ', / Wyb / match "Possibly YesterDay."' LY 'in "POSSIBLY YesterDay.". Character B Significance: Match a non-bound line of a word, for example: / WBN / Match 'on', / ybw / match "Possibly Yesterday." In "Possibly YesterDay." In "Noonday". Character CX Significance: The X here is a control character. Match a control character of a string. For example: / cm / matches 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 table single character n meaning: Match a wrap character R meaning: Match a ridicule character s significant: match a single White space character, including space, Tab, Form feed, newline, equivalent [FNRTV]. For example: / sw * / match 'Bar' in "Foo Bar.". Character s Significance: Match a single character other than WHITE spaces, equivalent to [^ Fnrtv]. For example: / s / w * Match "Foo Bar." In 'foo'.
Character T Size: Match a tabby character v meaning: match 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 '3' in "Apple," in "Apple,". " Character W Significance: Match other characters except numbers, letters and underscore, equivalent to [^ A-ZA-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 'in the "Apple, Orange, Cherry, Peach." There is a more complete example below. Note: If the number in the left wrapping brackets is smaller than the number specified, n is removed as a description of the octal ESCAPE. Character OOCTAL and XHEX meanings: OOCTAL here is an octa 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. The use of the text symbol can cause 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 separated JavaScript thread runs to get its own regexp object. Because each script is uninterrupted in a thread, this ensures that different scripts do not overwrite 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.
Properties $ 1, ..., $ 9 acquires matching substrings, if there is a property $ _ Reference INPUT property $ * Reference Multiline Property $ & Reference LastMatch Properties $ `Reference LEFTPAREN Property $` Reference LEFTCONTEXT Property $ 'Reference RightconText Property Constructor Specifies whether to build the 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 where the case is negligible when trying to match the string, when the regular expression is matched, for the opposite string. Attributes LastIndex decision When the next match starts from where the last property lastmatch last matches match, 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 object properties RightContext last matched substrings. Property Source Mode Text Method Compile Method Compiling a Regular Expression Object Exec method Run Regular Expression Matching Test Method Test Regular Matching TiSource Method Returns a text description of an object; you can use this value to create a new object . Does not consider the Object.tosource method. 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 method examples of the object: 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 the name of the $ attribute of the regexp object is passed to the Replace method as the second parameter.