Special symbols of JS regular expressions

Source: Internet
Author: User
Tags character classes

In Regular Expressions , many punctuation marks have special meanings, which are difficult to remember, and are now summed up in one part:

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

1. "[]" denotes a character class: that is, the parentheses are a character set: such as/[abc]/, the expression and the string containing any one letter of A,b,c match.

Note: Special character classes: \s represents space characters, tab characters, Unicode whitespace characters. \s represents a non-Unicode whitespace character. (You can also customize the Unicode character class:

/[\u4e00-\u9fa5]/indicates that only Chinese characters are matched. )

\w any single character, equivalent to [a-za-x0-9_];\w and \w.

\d any number, equivalent to [0-9];\d and \w opposite.

\b is used in character classes to represent BACKSPACE. [\b] Indicates the direct amount of the backspace. \b can be used to specify a matching position, or it can be called an anchor, such as:

Find the word Java in a sentence, you can use/\bjava\b/;\b instead.

2. "-" means hyphens, such as [A-z];

3. "." Represents any character except newline characters and other Unicode line terminators.

4. "^" has a twofold meaning: start tag and non, such as:/^a/represents a character starting with a, when in []:/[^a]/represents all characters other than a.

5. "$" indicates the end of the character. For example:/^abc$/represents a character ending with C.

6. "{}" means repeating the previous item. If/\d{2,4}/indicates that the number appears two times, it appears up to 4 times. such as/3{2,4}/match 33 ... ; 333 ..... ; 3333...; Mismatch between 3 and 4 3 is connected above. Its three formats are as follows {N,m} representing at least 3 times, up to M times, {n,} at least n times, and {n} exactly n times.

7. "?" Represents {0,1}.

8. "+" means {1,}.

9. "*" means {0,}.

Note: non-greedy duplicates (such as??, +?,*?,{1,5}? Match only the first one. )

10. "|" Represents the meaning of segmentation, even if. such as:/ab|cd|ef/matches a string containing AB or CD or EF.

11. "()" contains a threefold purpose: one is to define sub-expressions. The second is to define the sub-pattern in the complete pattern. The third is a reference to the subexpression.

The definition of a sub-pattern can be extracted from the target string and matched to the sub-pattern in parentheses.

A reference to the expression of a sub-pattern refers to a number that recognizes the sub-pattern and extracts it. such as/(ABC) \sis\s (string\w*)/; contains two words expression: can be

\1 means (ABC); \2 (string\w*); application as:/[' "] [^' "] *[' "] /cannot take the same quotation marks, can write:/[(' ") [^' "] *\1]/, can play a binding role.

Note: If you want to not be remembered, you can use (?: ...), it will not be numbered.

and (? =p), (?! p) as follows:

such as/(JavaScript)? (? =\:)/Indicates matching javascript:, but does not contain:; it does not match JavaScript, because he has a condition to be followed:;

(?! P) Anti-forward declaration, which requires that the next character does not match the pattern p, as opposed to (? =p).

Transferred from: http://www.cnblogs.com/devcjq/articles/2920112.html

Special symbols of JS 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.