Summary of regular expressions in JavaScript

Source: Internet
Author: User
Tags character classes constructor modifier modifiers regular expression split

Ways to define regular expressions

There are two ways to define a regular expression: the constructor definition and the regular expression direct quantity definition. For example:

The code is as follows

var reg1 = new RegExp (' D{5, 11} '); Through the constructor definition
var reg2 =/d{5, 12}/; Defined by direct quantity

Regular expression direct-measure character

O:nul character (u0000)

T: Tab (U0009)

N: NewLine character (u000a)

V: Vertical tabs (u000b)

F: Page breaks (u000c)

R: Return character (u000d)

XNN: Latin character specified by the hexadecimal number NN, for example, x0a equivalent to n

UXXXX: Unicode characters specified by the hexadecimal number xxxx, such as u0009 equivalent to T

CX: Control character ^x, for example, CJ is equivalent to line feed n

Regular expression anchor character

^: Matches the beginning of a string, in multiple-row retrieval, matching the beginning of a line

$: Matches the end of a string, in multiple-row retrieval, matching the end of a line

B: Match the bounds of a word, in short, the position between the character W and W, or the position between the character W and the beginning or end of the string ([B] matches the backspace)

B: position to match non word boundaries

(? =p): 0 wide forward lookahead assertion, requiring that the next character be matched to p, but not including those characters that match P

(?! P): 0 wide Negative lookahead assertion, requiring that the next string not match p

Character classes for regular expressions

[...] : Any character in square brackets

[^...] : Any character not in square brackets

.: Any character except line breaks and other Unicode line terminators

W: Word of any ASCII character, equivalent to [a-za-z0-9]

W: Any word that is not an ASCII character, equivalent to [^a-za-z0-9]

S: Any Unicode whitespace characters

S: Any characters that are not Unicode whitespace, note that W and s are different

D: Any ASCII number, equivalent to [0-9]

D: Any character other than the ASCII number, equivalent to [^0-9]

[b]: Backspace Direct quantity (special case)

Repeating character syntax for regular expressions

{N, M}: matches the previous item at least n times, but not more than m times

{N,}: matches the previous n or more times

{n}: match n times before

?: Matches the previous item 0 or 1 times, which means the previous item is optional, equivalent to {0, 1}

+: Matches the previous item 1 or more times, equivalent to {1,}

*: Matches the previous item 0 or more times, equivalent to {0,}

Selection, grouping, and reference characters for regular expressions

|: Select, match the subexpression on the left side of the symbol or the right side subexpression

(...) : Group, combining several items into one unit, which can be passed "*", "+", "?" and "|" , and you can remember the strings that match this group for any subsequent use

(?: ...) : group only, combine items into one unit, but do not memorize characters that match the reorganization

N: matches the first matching character of the nth group, the group is a subexpression (and possibly nested) in parentheses, and the group index is left to right parenthesis, and the "(?:" Form of grouping is not encoded

Regular expression modifiers

I: Performing case-insensitive matching

G: Perform a global match, in short, to find all matches, rather than stopping after the first one is found

M: multiple-line matching pattern, ^ matches the beginning of a line and the beginning of a string, $ matches the end of a line, and the end of a string

String method for pattern matching

Search (): its argument is a regular expression that returns the starting position of the first substring to match, and returns 1 if there is no matching substring. If the search () parameter is not a regular expression, it is first converted to a regular expression through the RegExp constructor, and search () does not support global retrieval because it ignores modifier g. Such as:

The code is as follows

var s = "JavaScript". Search (/script/i); s = 4

Replace (): It is used to perform retrieval and substitution. Receives two parameters, the first is a regular expression, and the second is the string to be replaced. If the regular expression is set modifier g, it is replaced globally, otherwise only the first substring of the match is replaced. If the first argument is not a regular expression, the string is searched directly, rather than converted to a regular expression. Such as:

The code is as follows

var s = "JavaScript". Replace (/java/gi, "Script"); s = Script Script

Match (): its argument is a regular expression, and if not, the RegExp conversion returns an array of matching results. If modifier g is set, a global match is made. Such as:

The code is as follows

var d = ' ff/hh '. Match (/d+/g); D = ["55", "33", "77"]

Split (): This method is used to split the string that calls it into an array of substrings, using a split () parameter, and its arguments can also make a regular expression. Such as:

The code is as follows

var d = ' 123,31,453,645 '. Split (', '); d = ["123", "31", "453", "645"]
var d = ', 123, N, 3 '. Split (/s*,s*/); D = ["21", "123", "44", "64", "67", "3"]

RegExp objects

Each RegExp object has 5 properties. The property source is a read-only string containing the text of the regular expression. The property Global is a read-only Boolean value that indicates whether the regular expression has a modifier g. The property ignorecase is a read-only Boolean value indicating whether the regular expression has modifiers I. The property multiline is a read-only Boolean value indicating whether the regular expression has a modifier m. The property lastindex is a readable, writable integer that stores the start of the next retrieval of the entire string if the matching pattern has a G modifier.

The RegExp object has two methods. The exec () parameter is a string that functions like match (), and the Exec () method executes a regular expression on a specified string, which is the execution of a matching search in a string. Returns null if no match is found, returns an array if a match is found, the first element of this array contains a string that matches the regular expression, and the remaining elements are substrings that match the subexpression within the parentheses, and the same array is returned regardless of whether the regular expression has a modifier g. When a regular expression object that calls exec () has a modifier g, it sets the Lastindex property of the current regular expression object to the character position that matches the substring next to it. When the same regular expression invokes exec () The second time, it retrieves from the string indicated by the Lastindex property, and if EXEC () does not find any matching results, it resets the lastindex to 0. Such as:

The code is as follows

var p =/java/g;
var text = "JavaScript is more fun than java! Www.111cn.net "
var R;
while ((R = p.exec (text))!= null) {
Console.log (R, ' lastindex: ' + p.lastindex);
}

Another method is test (), its argument is a string, a string is checked with test (), or false if a matching result of a regular expression is included, returns True. Such as:

  code is as follows &nbs P;

var p =/java/i;
P.test (' JavaScript ');//True

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.