Rethink about regular expressions (to be continued)

Source: Internet
Author: User

I recently encountered an error when using the G flag of a regular expression. Simply sum up the regular expression to improve understanding!

1. Create a regular expression

1.1 Use the regular expression literal (I .e. //) to create

VaR Reg =/(.) At/g;

1.2 use the constructor (Regexp)

VaR Reg = new Regexp ('(.) at', 'G ');

1.3 differences between the two methods

1.3.1. The expression contains metacharacters (for example, [, |, etc)

Former: var Reg =/\. at/g // match. At; latter: var Reg = new Regexp ('\. at', 'G ');

2. Usage of flag

The regular expression carries g to indicate that the regular expression is applied to the string that meets the condition, instead of stopping the match after the first match is found. In this case, the lastindex attribute changes with the use of the expression. For example:

Case 2:

3. for common regular expressions, refer to 1, 2.

Email: ^ [\ W-] + (\. [\ W-] +) * @ [\ W-] + (\. [\ W-] +) + $

URL: [http | HTTPS | FTP]: // [^ \ s] + Complex:/[http | HTTPS | FTP]: /// ([\ W-] + \.) ([\ W-] + )(\? [\ W = &] *)?

IP Address: (25 [0-4] | 2 [0-4] \ d | [01]? \ D ?) \.) {3} (25 [0-4] | 2 [0-4] \ d | [01]? \ D ?)

QQ: [1-9] \ D {4 ,}

HTML: <(^ [A-Z] [A-Z 0-9 "])> \ W * <\/\ 1> | <^ [A-Z] [A-Z 0-9 "] \/>

-Month-day: (^ [1-9] \ D {3})-(1 [0-2] | 0? [1-9])-(3 [0-1] | [12] [0-9] | 0? [1-9])

Chinese characters: [\ u4e00-\ u9fa5]

Chinese mobile phone number: 1 \ D {10}

Chinese mainland ID card number: ^ [1-9] \ D {14} $ | ^ [1-9] \ D {16} [1-9xx] $

Non-negative integer: \ D +

Positive Integer: ^ [1-9] \ D *

Negative integer: ^-[1-9] \ D *

Integer: ^ -? \ D *

Decimal :(-? \ D *) (\. \ D + )?

4. Use Cases with good Regular Expressions

View code

1. Compile a JavaScript function torgb to convert the commonly used color encoding in CSS. Requirements: 2 3 alert (torgb ("# 0000ff"); // output RGB (0, 0,255) 4 alert (torgb ("invalid ")); // output invalid 5 alert (torgb ("# g00"); // output # g00 6 7 function torgb (VAL) {8 var reg1 =/^ # ([0-9a-f] {2}) ([0-9a-f] {2}) ([0-9a-f] {2}) $/I; 9 var reg2 =/^ # ([0-9a-f]) ([0-9a-f]) ([0-9a-f]) $/I; 10 var reg3 =/[0-9a-f] {2}/g; 11 if (reg2.test (VAL) {12 val = Val. replace (reg2, "$1 $1 $2 $2 $3 $3"); 13} 14 15 if (reg1.test (VAL) {16 var match = Val. match (reg3); 17 val = 'rgb ('+ [parseint (Match [0], 16), parseint (Match [1], 16 ), parseint (Match [2], 16)]. join (',') + ')'; 18} 19 20 return val; 21}

 

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.