This article mainly introduces common PHP and javascript regular expressions and usage examples. carefully collected 10 PHP and javascript regular expressions each. if you need them, refer to the regular expressions below.
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 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 );