Summary of regular expressions in JavaScript

Source: Internet
Author: User
Tags character classes

Method for defining Regular Expressions

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

The Code is as follows:  

Var reg1 = new RegExp ('d {5, 11} '); // defined by the constructor
Var reg2 =/d {5, 12}/; // defined by direct quantity

Regular Expressions directly count characters

O: NUL character (u0000)

T: tab (u0009)

N: linefeed (u000A)

V: vertical tab (u000B)

F: Page feed (u000C)

R: Carriage Return (u000D)

Xnn: The Latin character specified by the hexadecimal number nn. For example, x0A is equivalent to n.

Uxxxx: Unicode Character specified by hexadecimal xxxx. For example, u0009 is equivalent to t.

CX: Control Character ^ X. For example, cJ is equivalent to line break n.

Regular Expression anchor character

^: Match the start of a string. In multi-row search, match the beginning of a line.

$: Match the end of a string. In multi-row search, match the end of a row.

B: match the boundary of a word. In short, it is the position between w and W, or the position between the start or end of character w and string ([B] matches the return character)

B: match the position of non-word boundary

(? = P): the zero-width forward assertions require that all subsequent characters match p, but not those that match p.

(?! P): The assertion is zero-width-negative and requires that the subsequent string does not match p.

Character classes of regular expressions

[...]: Any character in square brackets

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

.: Any character except the line break and other Unicode line terminator

W: Any word consisting of ASCII characters, equivalent to [a-zA-Z0-9]

W: Any word that is not composed of ASCII characters, equivalent to [^ a-zA-Z0-9]

S: Any Unicode white space character

S: any non-Unicode characters. Note that w and S are different.

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

D: any character except ASCII numbers, equivalent to [^ 0-9]

[B]: return the direct amount (Special Case)

Repeated character Syntax of a regular expression

{N, m}: match the first item at least n times, but cannot exceed m times

{N ,}: match the previous item n times or more times

{N}: match the first item n times

? : Match the first item 0 or 1, that is, the first item is optional, equivalent to {0, 1}

+: Match the previous item once or multiple times, equivalent to {1 ,}

*: Match the first item 0 or multiple times, equivalent to {0 ,}

Selection, grouping, and reference characters of Regular Expressions

|: Select. Match the child expression on the left or the child expression on the right of the symbol.

(...) : Combination, which combines several items into a Unit. This unit can be divided into "*", "+", "?" And "|", and you can remember the strings that match this group for any subsequent use.

(? :...) : Only combines items into a unit, but does not remember the characters matching the reorganization.

N: match the character that matches the nth group for the first time. The Group is a subexpression (or nested) in parentheses ), the group index is the number of left parentheses from left to right, "(? : "The group is not encoded.

Regular Expression Modifier

I: perform case-insensitive matching.

G: execute a global match. In short, find all the matches instead of stopping them after finding the first one.

M: multiline matching mode. ^ matches the beginning of a row and the start of a string, $ matches the end of a row and the end of a string.

String Method Used for Pattern Matching

Search (): its parameter is a regular expression that returns the starting position of the first matched substring. If no matched substring exists,-1 is returned. If the search () parameter is not a regular expression, it is first converted to a regular expression through the RegExp constructor. search () does not support global search because it ignores the modifier g. For example:

The Code is as follows:  

Var s = "JavaScript". search (/script/I); // s = 4

Replace (): it is used for retrieval and replacement. Receives two parameters. The first is a regular expression, and the second is a string to be replaced. If the modifier g is set for the regular expression, it is replaced globally. Otherwise, only the matching first substring is replaced. If the first parameter is not a regular expression, search for the string instead of converting it to a regular expression. For example:

The Code is as follows:  

Var s = "JavaScript". replace (/java/gi, "Script"); // s = Script

Match (): its parameter is a regular expression. If not, it is converted by RegExp, and an array composed of matching results is returned. If modifier g is set, global match is performed. For example:

The Code is as follows:  

Var d = '55 ff 33 hh 77 tt'. match (/d +/g); // d = ["55", "33", "77"]

Split (): This method is used to split the string that calls it into an array composed of substrings. The delimiter used is the split () parameter, and its parameters can also make a regular expression. For example:

The Code is as follows:  

Var d = '2014, 123 '. split (', '); // d = ["31,453,645", "31", "123", "453"]
Var d = '2014, 44, 64, 67, 3 '. split (/s *, s */); // d = ["21", "123", "44", "64", "67", "3"]

RegExp object

Each RegExp object has five attributes. The Attribute source is a read-only string that contains the text of a regular expression. The attribute global is a read-only Boolean value to indicate whether the regular expression carries a modifier g. The attribute ignoreCase is a read-only Boolean value used to indicate whether the regular expression carries the modifier I. The multiline attribute is a read-only Boolean value to indicate whether the regular expression carries a modifier m. The attribute lastIndex is a readable and writable integer. If the matching mode carries a g modifier, this attribute stores the start position of the next retrieval of the entire string.

The RegExp object has two methods. The exec () parameter is a string and its function is similar to match (). The exec () method executes a regular expression on a specified string, that is, to perform a matching search in a string. If no match is found, null is returned. If a match is found, an array is returned. The first element of the array contains a string that matches the regular expression, the remaining elements are substrings that match the subexpressions in parentheses. no matter whether the regular expression has a modifier g, the same array is returned. When the regular expression object that calls exec () has a modifier g, it sets the lastIndex attribute of the current regular expression object to the character location next to the matching substring. When the same regular expression calls exec () for the second time, it will start to search from the string indicated by the lastIndex attribute. If exec () does not find any matching results, it resets the lastIndex to 0. For example:

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.exe c (text ))! = Null ){
Console. log (r, 'lastindex: '+ p. lastIndex );
}

Another method is test (). Its parameter is a string. test () is used to check a string. if it contains a matching result of a regular expression, true is returned. Otherwise, false is returned. For example:

The Code is as follows:  

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.