JavaScript Express Learning Notes

Source: Internet
Author: User

First, the regular expression is what:

When working with strings, you often need strings that handle complex rules. Regular expressions are the tools used to describe these rules. In other words, the code that records the text rules.

Ii. What regular expressions can do:

Data validation (test string matching) such as mailbox, phone number, etc.

Replace text

Extract substring

Third, Characteristics:

Very flexible, logical and functional

Can quickly achieve complex control of strings in a very simple way

People who have just come into contact are more obscure.

Iv. rules

1. Ordinary characters

Function: Matches one character to the same.

Letters, numbers, kanji, underline.

2. Non-printable characters

\f: Matches a page break. Equivalent to \x0c and \CL.

\ n: Matches a line break. Equivalent to \x0a and \CJ.

\ r: Matches a carriage return character. Equivalent to \x0d and \cm.

\ t: matches a tab. Equivalent to \x09 and \ci.

\v: Matches a vertical tab. Equivalent to \x0b and \ck.

\s: Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].

\s: Matches any non-whitespace character. equivalent to [^ \f\n\r\t\v].

3. Special characters
Role: In regular expression processing, there are special meanings of characters.  

. (dot): matches any character except line break

\w: Match character number, equivalent to [a-za-z0-9]

\d: number, equivalent to [0-9]

\b: The beginning or end of a word. For example: "er\b" matches "er" at the second end of "Nerver" and does not match "er" of "verb" because it does not end with "ER"

^: start identification. For example, "^a\d" can match only "A1" strings that begin with the letter "a"

$: End identity. For example, "\da$" can only match "1a" with a string ending with the letter "a"

*: Match character 0 or more times, equivalent to {0,}. For example "Ab*c" can Match "AC", "abc", "ABBC" and so on between the letter "a" and "C" contains 0 or more "B". But cannot match "ABDC".

+: Matches one or more times, equivalent to {1,}.

?: Matches 0 or one time. Equivalent to {0,1}.

4. Escape character

Function: used when finding meta characters.

For example: Find "." Requires "\." Said. "*" → "\*". "\" → "\ \".

5. Repeat

Function: Indicates the number of repetitions of the same character.

"*", "+", "?" (see meta-character interpretation)

{n}: Repeats n times. For example, "Ab{3}c" can only match "ABBBC".

{N,}: repeats N or more times. For example {1,} equals "+" to repeat one or more times.

{n,m}: repeats N to M times.

6. Branch Conditions

| Any expression on either side is in effect.

For example: Match phone number, area code 3-bit or 4-bit, number 7-bit or 8-bit. "0\d{2}-\d{8}|0\d{3}-\d{7}"

7. Grouping

Use parentheses to formulate sub-expressions

Example: Match IP address, (\d{1,3}\.) {3}\d{1,3}. However, the disadvantage of this method is that you can match strings that do not conform to IP address rules, such as 300.500.123.900.

An expression that improves the ability to correctly match an IP address is "(2[0-4]\d|25[0-5]|[ 01]?\d\d?) \.) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?]

8. Back To reference

Function: When a subexpression is pinned with parentheses, a string matching the subexpression is stored and can be further processed in an expression or other program.

Rule: Left-to-right, identified by the left parenthesis of the group, the first occurrence of the grouping is 1 (denoted by "\1"), and so on, up to 99.

For example: "(\w) \1" will match two duplicate letters or numbers. "aabbc22344" will get "AA" "BB" "22" "44" without matching "C" and "3".

8.1. Auxiliary matching Group (0 wide assertion)

Function: As a match condition, but not included in the match result.

1, positive declaration (? =): The pattern in parentheses must appear to the right of the Declaration, but not as part of the match.

For example: "[\w\#]+ (? =\.net)" matches a string in the form of "text. NET". The result of "c#.net,vb.net,php,java,jscript.net" is "C # VB JScript".

2, Negative declaration (?!) : The pattern in parentheses must not appear on the right side of the declaration

For example: "\b\w*q (?! u) \w*\b "match the word with Q followed by not following U. The result of "remember AQA bqu ' abc '" Is "AQA".

3. Reverse positive declaration (? <=): The pattern in parentheses must appear to the left of the Declaration, but not as part of the match.

For example: "(? <=\bre) \w+\b" matches the word that begins with re, but the re does not return. The result of "public remember string ' abc '" Is "member".

4, Reverse negative declaration (?

For example: "\b\w*q (?! u) \w*\b "match the word with Q followed by not following U. The result of "remember AQA bqu ' abc '" Is "AQA".

Five, JS regular function Match,exec,test,search,replace,split

Because match and exec are very similar and easy to confuse, explain the difference first

1.exec is a method of a regular expression, not a method of a string, so the argument is a string.

For example: var reg = new RegExp ("abc");

var str = "3ABC4,5ABC6";

Reg.exec (str);

The result is: "ABC".

2.match is a string that performs a matching regular expression, so the parameter is a regular expression.

For example: var reg = new RegExp ("abc");

var str = "3ABC4,5ABC6";

Str.match (REG);

The result is: "ABC".

Write so much for a while and have time to continue updating.

JavaScript Express Learning Notes

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.