The JavaScript authoritative guide regular expression

Source: Internet
Author: User
Tags character classes uppercase letter

A direct volume is defined as a string that is contained directly in a pair of slashes

var pattern =/s$/;

Using the constructor RegExp () to define a regular expression equivalent to it

var pattern = new RegExp ("S $");

Direct Volume characters
Character The
alphabetic and numeric characters Their own
\o Nul characters
\ t Tabs
\ n Line break
\v Vertical tab
\f Page break
\ r Carriage return character

Character class

The character class is formed by placing direct volume characters in square brackets alone. A character class can match any character it contains.

Character The
[] Any character in square brackets
[^] Any character that is not in square brackets
. Any characters except line breaks and other Unicode line terminators
\w [A-za-z0-9]
\w [^a-za-z0-9]
\s Any Unicode whitespace character
\s Any non-Unicode whitespace character
\d [0-9]
\d [^0-9]
[\b] Backspace Direct Volume

Repeat
character meaning
{n,m} matches the previous item at least n times, but not more than m times
{n,} matches the previous item n times or more
{n} matches the previous item n times
matches the previous item 0 or one time, equivalent to {0,1}
+ match previous one or more times, equivalent to {1,}
* match previous Item 0 or more times, equivalent to {0,}

Non-greedy repetition

Match as few as you can

{n,m}?   ??   +? *?

Select, group, and reference

Select |

grouping () combining individual items into sub-expressions

Defines a sub-pattern in a complete pattern, and when a regular expression succeeds and the target string matches, the portion of the sub-pattern that can be extracted from the target string and the parentheses are matched

Allows you to refer to the preceding subexpression at the back of the same regular expression, \1 refers to the first parenthesized subexpression, where the position is the opening parenthesis of the participating calculation.

A reference to the preceding subexpression of a regular expression refers to a reference to the text that matches that pattern.

/([' "]) [^ '"]]*\1/

Regular expressions do not allow quotation marks around content enclosed in double quotes, and vice-versa. It is not allowed to use this reference in character classes.

Character Meaning
| Select to match the sub-expression to the left or right of the character
() Combine to combine several items into a single unit that can be passed *? + | modifier, and you can remember the string that matches this combination for subsequent reference use
(?:) Combine items into a single unit without remembering the characters that match the item
\ n Matches the first character of the nth grouping, the group is a subexpression of parentheses (possibly nested), the group index is left-to-right, the left parenthesis, (?: The form of grouping is not numbered

Specify a matching location /tr>
^ matches the beginning of the string, the multi-row retrieval, Matches the beginning of a line
$ Matches the end of the string, in multiple rows of the search, matches the end of a line
\b matches the boundary of a word, that is, the position between the character \w and \w. Or at the position between the character \w and the beginning or end of the string ([\b] matches the backspace)
\b Span style= "FONT-SIZE:14PX;" > matches the position of a non-word boundary
(? =p)
(?! p) 0 wide negative lookahead assertion, requiring the next character not to match P

/[jj]ava ([ss]cript)? (? =\:)/Can Match "Javascript" in ' javascript:the Definitive guide ', but not Java in ' Java in a nutshell ' because there is no quotation mark behind

/java (?! Script) ([a-z]\w*)/Can Match "Java" followed by an uppercase letter and any ASCII word, but behind Java cannot follow "Script". It can match "JavaBeans", but cannot match "Javanese", it can match "JavaScript", but it cannot match "Javascripter"

Modifier
Character Meaning
I Perform a case-insensitive match
G Perform a global match to find all matches, not stop after finding the first one
M Multiline matching pattern, ^ matches the beginning of a line and the beginning of a string, the end of the matching line and the end of the string

Modifiers can be placed in or out of any combination. To match the case to a word "Java" that matches a string without a case, use/\bjava\b/i, which matches all the words, using/\bjava\b/ig.

String method for matching patterns

Search ()

parameter is a regular expression that returns the starting position of the first substring to match, or 1 if not found.

If the parameter of search () does not say a regular expression, it is first converted to a regular expression through the RegExp constructor, and search () does not support global retrieval, and the g of the regular expression parameter is ignored.

Replace ()

Performs a retrieval and replace operation. The first argument is a regular expression, and the second argument is the string to replace.

If the regular expression has the modifier g set, all substrings that match the pattern are replaced with the string specified by the second argument, or only the first substring is substituted.

If the first argument is a string instead of a regular expression, the string is searched directly.

If the $ plus number appears in the replacement string, replace () replaces the two characters with the text that matches the specified subexpression.

Match ()

Returns an array that consists of matching results. If the regular expression sets the modifier g, the array returned by the method contains all the matching results in the string.

"1 plus 2 equals 3". Match (/\d+/g)//Return ["1", "2", "3"];

If there is no modifier g,match () retrieves only the first match, but still returns an array, the first element of the array is the matching string, and the remaining elements are sub-expressions in parentheses around the regular expression.

varurl =/(\w+): \/\/([\w.] +) \/(\s*)/;varText = "Visit my blog at Http://www.example.com/~hehe";varresult =text.match (URL);if(Result! =NULL) {    varFullUrl = result[0];//Http://www.example.com/~hehe    varprotocol = result[1];//http    varhost = result[2];//www.example.com    varPath = result[3];//~hehe}

Split ()

Splits the string that invokes it into an array of substrings that use the split () argument.

"1, 2, 3, 4, 5". Split (/\s*,\s*/); return ["1", "2", "3", "4", "5"]

RegExp Object

The RegExp constructor has two string arguments, and the second parameter is optional. The first parameter contains the body of the regular expression, which is the text between//. The second parameter specifies a modifier.

You need to use the RegExp () constructor when you need to create a regular expression dynamically.

RegExp Property

Source: Read-only, text containing regular expressions

Global: Read-only Boolean value that indicates whether the modifier g

IgnoreCase: Read-only Boolean value that indicates whether the modifier i

Multiline: Read-only Boolean value that indicates whether the modifier m

LastIndex: can read and write integers, if the matching pattern with the G modifier, where the property is stored in the entire string at the beginning of the next retrieval

RegExp method

EXEC ()

Executes a regular expression on a specified string, if no match is found to return null, if a match is found, an array is returned.

The first element of this array is a string that matches the regular expression, and the remaining elements are substrings that match the subexpression within the parentheses.

The property index contains the character position where the match occurred, and the property input refers to the string being retrieved.

Test ()

Detects a string and returns True if it contains a matching result of a regular expression

var pattern =/java/i;

Pattern.test ("Javascirpt"); True

When a global regular expression invokes the method test (), its behavior is the same as exec () because it retrieves a string starting at the location specified by lastindex, and if a matching result is found, set lastindex immediately to the end of the current matched substring. You can use test () to iterate through the string, just as you would with the Exec () method.

The JavaScript authoritative guide regular expression

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.