Regular expression of JavaScript

Source: Internet
Author: User

Two ways to create regular expressions

an explicit :

New RegExp("pattern" [, "flags"]);

Cases

var regex = new ("abc", "GI");

The first parameter is the pattern to match, the second is the identity information, and there are three identifying information:

G: Global identity, which indicates that all matching parts of the text function, if not set, only search and match the first occurrence of the text segment global

I: Ignore case ID IgnoreCase

M: Multi-line identification, if this is not set, then the metacharacters "^" and "$" match only the start and end positions of the text, and if this identity is set, it can also match the position after "\ n", "\ R" in the string, that is, the beginning of the next line and the end of the line MultiLine

Implicit :

/pattern/[flags] No Quotes

Cases

var regex =/\w+/i;var regex = new RegExp ("\\w+", "I");

The above two are equivalent, when using an explicit constructor, the special symbols in the pattern string should use the escape character, that is, the "\" leading symbol

Pattern Match character

\ indicates that the following characters are special characters

^ Matching characters must be in the front

The $ match character must be in the last face

* match the preceding characters 0 or n times

+ at least 1 times

? 0 or 1 times

. Match all single characters except line break

X|y match x or Y

(x) mark the start and end position of a sub-expression

{n} matches the preceding n characters

{N,} matches at least n characters preceding

{n,m} matches the preceding n to M characters

[XYZ] matches any of the listed characters

[^XYZ] matches the complement of any of the listed characters, that is, any character other than the listed character

[a-d] matches any character in ABCD

[\b] matches a space

\b matches the dividing line of a word, such as a space

\b matches the non-dividing line of a word

\d matches a number, equivalent to [0-9]

\d matches a non-numeric

\f matches a single form character

\ n matches a line break

\ r matches a carriage return character

\s matches an empty Tan Fu, including spaces, tabs, feeds, form, line breaks, equivalent to [\f\n\r\t\v]

\s Match a tab

\w matches all letters, numbers, and underscores, equivalent to [a-za-z1-9_]

\w matching \w of the complement set

Greedy match and non-greedy match

Regular expression default greedy match, even with the longest matching principle

Example to match "Bo" in "book" Replaced with "L", replacing the result with "Lok" instead of "look"

When the "?" Use non-greedy mode immediately after any other qualifier (*, + 、?、 {n}, {n,m})

Example has the string "Booook", "bo+?" Matches only the "Bo" section, while "bo+" matches "boooo"

Select the match character

| The two options are the largest possible expression on both sides

Example "ABCD|EFGH2" matches "ABCD" or "EFGH" instead of "abcd2" or "EFGH2", if you want to match "ABCD2" or "EFGH2", You should create "(ABCD|EFGH) 2"

Special characters

If you want to match the literal meaning of the following special characters, you need to escape with "\"

^    $     *     +     ?      .     (      )      [      ]      {      }      | \      /

Static properties of regular expressions

(1)index property. is the starting position of the first match for the current expression pattern, counting from 0. Its initial value is-1, and the Index property changes each time a successful match is made.

(2)input property. Returns the currently-acting string, abbreviated to $_, and the initial value as an empty string "".

(3)LastIndex property. Is the next position of the last character in the first match of the current expression pattern, counting from 0, which is often used as the starting position to continue the search, with an initial value of-1, indicating that the search starts from the starting position, and the value of the Lastindex property changes each time a successful match is made.

(4)lastmatch property. is the last matching string for the current expression pattern, which can be abbreviated to $&. Its initial value is an empty string "". The value of the Lastmatch property changes with each successful match.

(5)Lastparen property. If there are sub-matches enclosed in the expression pattern, the substring that matches the last sub-match in the current expression pattern can be abbreviated to $+. Its initial value is an empty string "". The value of the Lastparen property changes every time a successful match is made.

(6)leftcontext property. Is all the content to the left of the last matching string in the current expression pattern, which can be abbreviated to $ ' (where "'" is the inverted single quotation mark below the "ESC" on the keyboard). The initial value is an empty string "". Each time a successful match occurs, its property value changes.

(7)rightcontext property. Is all content to the right of the last matching string of the current expression pattern, which can be abbreviated to $ '. The initial value is an empty string "". Each time a successful match occurs, its property value changes.

(8)$1...$9 property. These properties are read-only. If there are sub-matches enclosed in the expression pattern, the $1...$9 property values are captured by the 1th to 9th Sub-match. If there are more than 9 sub-matches, the $1...$9 attribute corresponds to the last 9 sub-matches, respectively. In an expression pattern, you can specify any number of parenthesized sub-matches, but the RegExp object can store only the results of the last 9 sub-matches. In the result array returned by some methods of the RegExp instance object, you can get all the sub-matching results within the parentheses.

Instance Properties

(1)Global properties. Returns the state of the global flag (g) specified when the RegExp object instance was created. If the G flag is set when the RegExp object instance is created, the property returns True, otherwise false and the default value is False.

(2)ignoreCase property. Returns the state of the ignorecase flag (i) that was specified when the RegExp object instance was created. If the I flag is set when the RegExp object instance is created, the property returns True, otherwise false and the default value is False.

(3)MultiLine property. Returns the state of the multiline flag (m) specified when the RegExp object instance was created. If the M flag is set when the RegExp object instance is created, the property returns True, otherwise false and the default value is False.

(4)source property. Returns the expression text string specified when the RegExp object instance was created.

Methods for RegExp objects

Test method

The syntax format is Test (str). This method checks whether there is an expression pattern specified when creating an RegExp object instance in a string, and returns True if it exists, otherwise false. If a match is found, the relevant static property in the RegExp object is updated to reflect the match.

Exec method

The syntax format is exec (str). The method searches for a string using the expression pattern specified when the RegExp object instance is created, and returns an array containing the search results.

If you set the global flag (g) for a regular expression, you can search the string continuously by calling the Exec and test methods multiple times, each time starting from the location specified by the Lastindex property value of the RegExp object.

If the global flag (g) is not set, the Exec and test methods ignore the value of the Lastindex property of the RegExp object, starting at the beginning of the string.

If the Exec method does not find a match, the return value is null, if a match is found, an array is returned, and the static property in the RegExp object is updated to reflect the matching situation. Element 0 in the returned array contains the complete match result, and the element 1~n in turn is the result of each sub-match defined in the expression pattern.

The array returned by the Exec method has 3 properties, namely input, index, and lastindex.

The input property is the entire string being searched for.

The Index property refers to the position of the match within the entire searched string.

The Lastindex property refers to the next character position of the last character of the matched substring.

Regular expression of JavaScript

Related Article

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.