Tips for using JavaScript Regular Expressions (2)

Source: Internet
Author: User

I. character classes

Concept:Put the Individual Direct character in square brackets to form a character class ).

Note:Red indicates that it is not particularly clear

[...] -- Indicates that any character in the character class meets

[^...] -- Except for any character in the character class

. -- Any character except the linefeed (\ n) and Unicode terminator (I do not know what to use ?)

\ W -- (Word) any ASCII single character, = [a-zA-Z0-9]

\ W -- any non-ASCII single-character, = [^ a-zA-Z0-9]

\ S -- (Space) Any Unicode white space characters

\ S -- any non-Unicode white space characters

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

\ D -- except ASCII numbers, = [^ 0-9]

[\ B] -- returns the amount directly (I don't know what it is ?)

Ii. Repetition

There are also {n, m}, {n ,}, {n },?, +, * Not explained one by one

Note: duplicate copies match one or more copies of the previous mode.

3. Non-Greedy repetition (I can understand it, but I cannot understand it after the experiment !)

CopyCode The Code is as follows: var S = "aaaaabab ";
VaR tanlan =/a * B /;
VaR landu =/*? B /;
VaR T = S. Match (tanlan); // thought t: aaaaabab, L: aaaaaaab
VaR L = S. Match (landu); // The two matched arrays are actually the same.

4. Selection, grouping, and reference

|: Select

The expression that matches the symbol from left to right (it will not go back after matching one)

VaR regchoose =/\ D {2} | \ W {4}/; // either two decimal places or four lower-case letters. matching the first one does not match the second one.

(): Combination

Role 1: merge a separate project group into a subexpression

VaR regcombination =/(cainiao eats insects) + insects eats vegetables // "cainiao eats insects" must appear once or more times

Role 2: retrieve the string and match it later


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.