Introduction and use of JavaScript regular expressions

Source: Internet
Author: User

Know the regular expression has been a long time, roughly will understand some, but there was no systematic study, recently in the reading "JS authoritative guide", just see the regular expression of the part, the comparison system of learning the regular expression.

Let's start with some basic information about regular expressions.

Defined

The direct amount of a regular expression is defined between a pair of slashes (/)

Direct volume character (front 7 senses will be more than one)

Letter or number (self)

\o (null character)

\ n (line feed)

\ t (tab)

\v (Vertical tab)

\f (page break)

\ r (Carriage return)

\XNN (16 binary number nn specified Latin character)

\UXXXX (16 binary number xxxx specified Unicode character)

\CX (Control character ^x)

Character class

[...] (any character in parentheses)

[^...] (without any characters in parentheses)

. (Any character except newline characters 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 characters,\w and \s differ )

\d ([0-9])

\d ([^0-9])

[\b] (backspace direct volume) special case

repeating characters

{n,m} (matches the previous item at least n times up to M times)

{N,} (matches the previous item at least n or more times)

{n} (matches the previous item exactly n times)

? (Match previous 0 or 1 times, equivalent {0,1})

+ (Match previous item 1 or more times, equivalent {1,})

* (Match previous item 0 or more times, equivalent {0,})

Non-greedy repetition

If/a+/matches 1 or more a,/a+?/matches only one A (the former is greedy, the latter is non-greedy)

Select, Group, reference

| (selector,/a|b/can match A or b)

(...) (Combination, define a sub-pattern, \1 represents the first parenthesized subexpression, and when nested, press the opening parenthesis/([']]) [^\1]*\1/when the first one is a single quote, \1 is a single quote, the other way around)

(?:...) (combination only, cannot be referenced by \1, that is, characters that match the array are not remembered)

\ n (matches the first character of the nth grouping)

Anchor character

^ (matches the beginning of a string, multiple rows are retrieved at the beginning of a row)

$ (matches the end of a string, and multiple rows are retrieved at the end of a row)

\b (matches the boundary of a word)

\b (matches non-word boundaries)

(? =p) (forward declaration, requires that the next character will match the pattern P)

(?! p) (Anti-forward declaration, requires the next character not to match the pattern P)

Mark (outside the/symbol description)

I (perform case insensitive match)

G (performs a global match, that is, all matches are found)

m (multi-line matching)

String method for matching patterns

Seach (REGEXP)

The method has a parameter, which is a regular expression, and if it is not a regular expression, it is also loaded with the RegExp constructor as a regular expression. Seach () does not support global retrieval, so flag G is ignored

Replace (regexp,string)

The first parameter of the method is a regular expression, and the second argument is the string to be replaced. When there is a flag G, the second parameter replaces all substrings that match the regular expression, and if there is no flag G, replaces the first substring that matches the pattern. If the first argument is a string, the string is retrieved directly.

The $n represents the nth substring. String=<aaa><bbb>;var X=string.replace (REGEXP, "<$1><$1>"); The value of X is <aaa><aaa>.

Match (REGEXP)

When there is a flag G, the method returns an array containing the matching results. When there is no flag g, an array is returned, Array[1] is the first substring.

Split (x)

Decomposition of the called string into a substring array, the delimiter is a parameter, parameters can also use regular expressions, such as/\*,s\s*/, commas have one or more spaces around

RegExp Object

Constructor RegExp (STRING,GIM), the first argument is a string for the body of the regular expression, note the use of the escape character, and the second parameter is the flag of a regular expression, which can be omitted.

RegExp Object Methods

Regexp.exec (String);

This method is similar to the match () method, but returns null if no match is found, regardless of the flag G, and if found, the method returns only the first matched string. If there is a flag G, the second call to exec is retrieved from the character position indicated by Regexp.lastindex, which iterates through all matches, it can achieve the result of match () plus the flag G, but the result does not return an array like match ().

Regexp.test (String), if the string contains a match for a regular expression, returns ture, when a global method calls Test (), the same regexp.lastindex is set to the position of the character after the match, so that the test () Methods can also be traversed.

RegExp Instance Properties (5)

SOURCE read-only regular expression text

Global read-only boolean indicating if there is a flag g

IgnoreCase read-Only boolean indicating if there is a flag I

Multiline read-only boolean indicating whether there is a flag m

LastIndex Readable and writable integers

Introduction and use of JavaScript regular expressions

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.