Simple summary of JS regular expression

Source: Internet
Author: User
Tags alphabetic character character classes

    • Regular expression definitions

Regular expressions in JavaScript are represented by RegExp objects, and you can use the RegExp () constructor to create RegExp objects, but the RegExp objects are created more by a special direct amount.

For example: Var pattern=/s$/;

Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.

Syntax for creating REGEXP objects:

New REGEXP (pattern, attributes);

    • Regular expression syntax

? Direct Volume characters

In regular expressions, letters and data are matched by literal meanings. JavaScript Regular expressions also support non-alphabetic character matching, which needs to be escaped by a backslash (\) as a prefix.

Character The
Letters and Numbers Their own
\o Nul characters
\ t Tabs
\ n Line break
\v Vertical tab
\f Page break
\ r Carriage return character
\xnn Latin characters specified by the hexadecimal nn
\uxxxx The Unicode character specified by hexadecimal xxxx
\cx Control character CX

Special meaning of punctuation, also need \ for the prefix to match:

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

? Character class

Regular Expression character classes

Character The
[...] Any character in square brackets
[^ ...] Any character within the square brackets
. Any character other than line breaks and other Unicode line terminators
\w A 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 character whitespace
\s Any character with non-Unicode character whitespace, note that \s differs from \w
\d Any ASCII number, equivalent to [0-9]
\d Any character other than an ASCII number, equivalent to [^0-9]
[\b] Backspace Direct Volume (special case)

? Repeat

Repeating character syntax for regular expressions

Character Meaning
{N,m} Matches the previous item at least n times, but no more than m times
{N,} Matches the previous item n or more times
N Matches the previous n times
? Matches the previous item 0 or 1 times, equivalent to {0,1}
+ Match the previous item at least 1 or more times, equivalent to {1,}
* Match the previous item at least 0 or more times, equivalent to {0,}

Note: * and? With caution, there may be a mismatch between what is allowed.

(Supplemental: Non-greedy match: The above repetition, which we call greedy match.) A non-greedy match is where the word specifier to be matched follows a question mark. "??", "+?" "," {1,5}? ”,? The expression is matched to a shorter match as possible when matching a string. )

? Select, group, and reference

() The effect of parentheses:

1. Combine individual items into sub-expressions so that you can work with a separate unit like "|", "*", "+" or "? ”

For example:/(AB|CD) +|ef/: Indicates that the string "EF" can be matched, or the string "AB" or "CD" can be matched one or more times

2. Defining a sub-pattern in a complete pattern

3. Allows you to reference the preceding subexpression at the back of the same regular expression, by adding a number to the character "\".

For example:/([' ""][^ ' "]*\1)/:\1 matches the pattern that matches the first parenthesized sub-expression. In such an expression, the quotation marks on the left side must match the quotation marks on the right.

                        does not appear as a single double quote string.

Selection, grouping, and referencing characters of regular expressions
Character Meaning
| Select to match the subexpression to the left of the symbol or to the right sub-expression
(...) Combine to combine several items into a single unit that can be "|", "*", "+" or "? , and you can remember the string that matches this combination for later reference
(?:...) Combine items into a single unit, but do not remember characters that match the group
\ n Matches the first character of the nth grouping, the group is a subexpression of parentheses (or it can be nested), and the group index is the left-to-right number of open parentheses ("?"). : "Form of grouping does not encode

? Specify match character position

Anchor characters in regular expressions

Character Meaning
^ Matches the beginning of a string, in a multi-row search, matches the beginning of a line
$ Matches the end of a string, in a multi-row search, matches the end of a line
\b Matches a simple edit, 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 (note that [\b] matches backspace)
\b Match the position of a non-word boundary
(? =p) 0 wide forward first phrase, which requires the next character to match p, but cannot include those characters that match P
(?! P 0 wide negative forward phrase that requires the next character not to match P

    • Modifier
Character Meaning
G Performs a global match (finds all matches rather than stops after the first match is found).
I Performs a match that is not case sensitive.
M Perform multi-line matching

    • Common regular Expressions (note must be written in English state):

Verification Number: ^[0-9]*$

To verify N-bit numbers: ^\d{n}$

Verify that at least n digits: ^\d{n,}$

Verify the number of m-n bits: ^\d{m,n}$

Verify numbers starting with 0 and non 0: ^ (0|[ 1-9][0-9]*) $

Verify that there is a positive real number with two decimal places: ^[0-9]+ (. [ 0-9]{2})? $

Verify that there is a positive real number with 1-3 decimal places: ^[0-9]+ (. [ 0-9]{1,3})? $

Verify non-zero positive integers: ^\+? [1-9] [0-9]*$

To verify a nonzero negative integer: ^\-[1-9][0-9]*$

Validating non-negative integers (positive integers + 0): ^\d+$

Validates a non-positive integer (negative integer + 0): ^ ((-\d+) | ( 0+)) $

Verify the character with a length of 3: ^. {3}$

Validates a string consisting of 26 English letters: ^[a-za-z]+$

Validates a string consisting of 26 uppercase English letters: ^[a-z]+$

Validates a string consisting of 26 lowercase English letters: ^[a-z]+$

Validates a string consisting of a number and 26 English letters: ^[a-za-z0-9]+$

Validates a string consisting of a number, 26 letters, or underscores: ^\w+$

Verify User password: ^[a-za-z]\w{5,17}$

The correct format is: Start with a letter, the length is between 6-18, and can contain only characters, numbers, and underscores.

Verify that it contains ^%& ',; =?$\ ' characters:[^%& ', =?$\x22]+

Verify Kanji: ^ ([\u4e00-\u9fa5]{0,}) $

Verify Email Address: ^\w+ ([-.] \w+) *@\w+ ([-.] \w+) + (\.\w+[-.) \w+) *$

Verify InternetURL:

^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? $; ^[a-za-z]+://(w+ (-w+) *) (. ( w+ (-w+) *) * (? s*)? $

Verify phone Number:

^ (\ (\d{3,4}\) |\d{3,4}-)? \d{7,8}$:--the correct format is: xxxx-xxxxxxx,xxxx-xxxxxxxx,xxx-xxxxxxx,xxx-xxxxxxxx,xxxxxxx,xxxxxxxx.

Verify your Social Security number (15-bit or 18-digit number): ^\d{15}|\d{18}$

Validation 12 months of the year: ^ (0?[ 1-9]|1[0-2]) $ correct format: "01"-"09" and "1" "12"

Verify one months of 31 days: ^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ The correct format is: 01, 09 and 1, 31.

Simple summary of JS regular expression

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.