Regular Expressions (PHP and Javscript) and javascript Regular Expressions are commonly used in WEB development.

Source: Internet
Author: User
Tags php regular expression preg

Regular Expressions (PHP and Javscript) and javascript Regular Expressions are commonly used in WEB development.

In WEB development, regular expressions are usually used to detect and search for strings that meet the rules, such as checking whether the user input E-mai format is correct and collecting page content that meets the rules.
Next we will use PHP and Javscript to introduce the most common and useful regular expressions in WEB development and their usage.

Common PHP expressions
1. match a positive integer:/^ [1-9] \ d * $/
2. match a non-negative integer (positive integer + 0):/^ \ d + $/
3. Match Chinese characters:/^ [\ x {4e00}-\ x {9fa5}] + $/u
4. matching Email:/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*/
5. Match URL :( (f | ht) {1} (tp | tps): //) [-a-zA-Z0-9 @: % _ \ + .~ #? & // =] +)
6. Match the start of a letter, 5-16 characters, letter and digit underline:/^ [a-zA-Z] [a-zA-Z0-9 _] {} $/
7. Match numbers, letters, underscores, Chinese:/^ [\ x {4e00}-\ x {9fa5} A-Za-z0-9 _] + $/u
8. Match the Chinese zip code:/^ [1-9] \ d {5} $/
9. Matched IP Address:/\ B (? :(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \.) {3 }(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \ B/
10. match Chinese mainland ID card:/^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} (\ d | x | X) $/
PHP Regular Expression verification string method example:

$ Str = "Chinese"; $ preg = "/^ [\ x {4e00}-\ x {9fa5}] + $/u "; // match Chinese if (preg_match ($ preg, $ str, $ arr) {$ msg = 'match successful! ';} Else {$ msg =' match failed! ';} Echo $ msg;

Common Javascript expressions
1. match a positive integer:/^ [0-9] * [1-9] [0-9] * $/
2. match a non-negative integer (positive integer + 0):/^ \ d + $/
3. Match Chinese characters:/^ [\ u4e00-\ u9fa5]/
4. matching Email:/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*/
5. URL matching URL:/^ (f | ht) {1} (tp | tps): \/([\ w-] + \.) + [\ w-] + (\/[\ w -. /? % & =] *)? /
6. Match the start of a letter, 5-16 characters, letter and digit underline:/^ [a-zA-Z] [a-zA-Z0-9 _] {} $/
7. Match numbers, letters, underscores, Chinese:/^ [\ u4e00-\ u9fa5A-Za-z0-9 _] + $/
8. Match the Chinese zip code:/^ [1-9] \ d {5} $/
9. Matched IP Address:/\ B (? :(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \.) {3 }(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \ B/
10. match Chinese mainland ID card:/^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} (\ d | x | X) $/
Example of the string method for Javascript Regular Expression Verification:

Var str = "abc@126.com"; var preg =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) */; // match Email if (preg. test (str) {var msg = "matched successfully";} else {var msg = "matched failed! ";} Alert (msg );

I want to help you learn the 20 common expressions compiled by Alibaba Cloud.

Regular Expressions are a subject and cannot be explained in an article. There are many theoretical things on the Internet. If you are interested, you can search and learn more.

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.