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:
Copy codeThe 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:
Copy codeThe 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 );