JavaScript Regular Expressions

Source: Internet
Author: User
Tags character classes

RegExp Object

The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.

Direct volume syntax
/pattern/attributes
Syntax for creating REGEXP objects:
New RegExp (patternattributes);
Parameters
pattern is a string that specifies the pattern of a regular expression or other regular expression. In a string, RegExp is a special character. We must escape ("/") if we want to use it!

The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

* Regular expressions are composed of two basic characters:

    1. literal literal characters
    2. Metacharacters

* Metacharacters are non-alphabetic characters that are unique in regular expressions

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

1. "[]" denotes a character class: that is, in parentheses is a character set, indicating the scope of: such as/[abc]/, the expression and contains any one letter of the string are matched, in the [] composition of the class can be ligatures, such as: [A-za-z].

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, as long as you add a question mark after the quantifier. )

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).

12. Grouping:

Reverse reference:

Prospect:

Methods of regular Expressions:

RegExp.prototype.test ()

--------------------------------------------------------------------------------------------------------------- ------------------

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.