Common regular expressions and usage in PHP and javascript _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression preg
Examples of common regular expressions and usage in PHP and javascript. In computer science, regular expressions are used to describe or match a single string that complies with a certain syntactic rule. In WEB development, regular expressions are usually used to detect, and in computer science, regular expressions are used to describe or match a series of individual strings that conform to a certain syntax rule. 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.
Today, we use PHP and Javscript to introduce the most common and useful regular expressions in WEB development and their usage. 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 for them. However, you may not have to study the incomprehensible regular expressions. read this article and examples to show you common and practical regular expressions.

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:

The code is as follows:


$ Str = "Chinese ";
$ Preg = "/^ [\ x {4e00}-\ x {9fa5}] + $/u"; // Match Chinese characters
If (preg_match ($ preg, $ str, $ arr )){
$ Msg = 'matched! ';
} Else {
$ Msg = 'matching 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:

The code is as follows:


Var str = "abc@126.com ";
Var preg =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) */; // Match Email
If (preg. test (str )){
Var msg = "matched ";
} Else {
Var msg = "matching failed! ";
}
Alert (msg );

Bytes. In WEB development, regular expressions are usually used to detect ,...

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.