20140829 share the regular expression outline

Source: Internet
Author: User
1. Regular Expression

^ Starts with [^] Not

$ End (per line)

\ Escape

. Match other words except \ n

? Previous 0 or 1 time

* The previous 0 + times

{N} n times

{N,} n + times

{N, m} n-m times

| Or

[123] [123] Set, [123] matching is not valid

[1-3] [A-Z] [A-Z] Set Range

[^ 123] [^ A-Z] The set is not

\ D 0-9

\ D non-numeric

\ N \ r

\ S is empty, including \ f \ n \ r \ t \ v

\ S is not empty

() Group

2. Regular Expression Composition

/Regular/Modifier

New Regexp ('reg', 'modifier ');

Modifier:

I is case insensitive

G Global

M multiple rows (if enabled, ^ $ indicates the target of each behavior)

3. Combination

^ 1 [3-8] \ D {9} $

^ \ D {7} $7 digits

^ (\ D {4 })? \ D {7} $ Add a four-digit area code

^ (\ D {4 }\-?)? \ D {7} $ Add a four-digit area code-

^ (0 \ D {3 }\-?)? \ D {7} $ must start with 0

^ (0 \ D {3 }\-?)? \ D {7} | (0 \ D {2 }\-?)? \ D {8}) $3-digit area code

^ (\ 0 \ D {3 }\-)? [1-9]) | (0 \ D {2 }\-)? [1-9] \ D) \ D {6} $3 4 // incorrect, Example

4. Greedy

1 (.*)? 0 => 10,110,100.

1 (.*?) 0 => 132323230, 1 ultra-large scale fsdfsdf0

Comment out:

<! \-([\ S] *?) \->

\/\/(.*?) $

Tag removal:

<\/? ([A-Za-Z] +?)>

5. assertion/pre-check/non-capture

? : Non-capturing

? = Forward judgment

?! Positive/false Determination

? <= Reverse determination (not supported by JS)

? <! Reverse no determination (not supported by JS)

1. Non-capturing

(? : \ D) [A-Za-Z] + // The group of numbers will not be captured later. It is usually used for match and exec. Non-capture can improve performance to some extent.

2, must be letters + numbers

^ (?! \ D + $ )(?! [A-Za-Z] + $) [0-9a-za-z] {1,} $

=> Convert

^

(?! \ D + $) // not end with a full number

(?! [A-Za-Z] + $) // not end with a full letter

[0-9a-za-z] {1,} // number + more than 1 letter

$

3. It must contain @ letters or numbers.

^ (? =. * @. *) [[Email protected] {2,} $

4. It must contain @, but cannot start or end

^ (? = .*@.*)(?! ^ @(.*))(?! (. *) @ $) [[Email protected] {2,} $

=>

^

(? =. * @. *) // Must contain @
(?! ^ @ (. *) // Cannot start @
(?! (. *) @ $) // It cannot end @
[[Email protected] {2,} // two or more letters or numbers @

$

6. Regular Expression

Test to verify whether the matching is successful

Exec is similar to string. match, but it is said there is a difference

/REG/. Test (value)

/REG/ig.exe C (value)

String. Match (/REG/g)

String. Split (/REG /)

7. Application scenarios

1. Path for collecting images: (the idea of js php is the same)

VaR STR = 'capture all the source code of the page to fffdsfsd fsdfsd fsdf ';
VaR arr = Str. Match (/] +?> /G) | []; // capture all image tags first
Arr. foreach (function (VAL) {// traverses all the results. Here Val gets the tag STR for each image.
VaR STR = Val. Match (/src = ['"]? (. + ?) ['"]? /) | ['',''];
Alert (STR [1]);

});

2. Password Complexity

VaR pass = 'fsdfsdf ';
VaR arr = pass. Match (/^ (? :( \ D +) | ([A-Z] +) | ([A-Z] +) | ([A-Za-Z] +) | ([a-zA-Z0-9] + )) $ /);
Arr [0] // Summary Result
Arr [1] // pure number
Arr [2] // lowercase letters only
Arr [3] // uppercase letters only

20140829 share the regular expression outline

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.