In web development, regular expressions are often used to detect, find, and replace certain strings that conform to rules, such as detecting the correctness of user input E-mai formats, gathering page content that matches the rules, and so on.
Here are some of the most common and useful regular expressions and their usage in web development, using PHP and Javscript, respectively.
PHP Common expression Usage
1. Matching positive integer:/^[1-9]\d*$/
2. Matching non-negative integer (positive integer +0):/^\d+$/
3. Match Chinese:/^[\x{4e00}-\x{9fa5}]+$/u
4. Match email:/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * * *
5. Matching URL: ((f|ht) {1} (TP|TPS)://) [-a-za-z0-9@:%_\+.~#?&//=]+)
6. Match the beginning of the letter, 5-16 characters, alphanumeric underline:/^[a-za-z][a-za-z0-9_]{4,15}$/
7. Matching numbers, letters, underscores, Chinese:/^[\x{4e00}-\x{9fa5}a-za-z0-9_]+$/u
8. Matching China postal code:/^[1-9]\d{5}$/
9. Matching 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. Matching 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) $/
Examples of PHP Regular validation string methods:
$STR = "Chinese ah";
$preg = "/^[\x{4e00}-\x{9fa5}]+$/u"; Match Chinese
if (Preg_match ($preg, $str, $arr)) {
$msg = ' Match succeeded! ';
} else{
$msg = ' match failed! ';
}
Echo $msg;
JavaScript Common expression Usage
1. Matching positive integer:/^[0-9]*[1-9][0-9]*$/
2. Matching non-negative integer (positive integer +0):/^\d+$/
3. Match Chinese:/^[\u4e00-\u9fa5]/
4. Match email:/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * * *
5. Matching URL url:/^ (f|ht) {1} (TP|TPS): \/\/([\w-]+\.) +[\w-]+ (\/[\w-/?%&=]*)?
6. Match the beginning of the letter, 5-16 characters, alphanumeric underline:/^[a-za-z][a-za-z0-9_]{4,15}$/
7. Matching numbers, letters, underscores, Chinese:/^[\u4e00-\u9fa5a-za-z0-9_]+$/
8. Matching China postal code:/^[1-9]\d{5}$/
9. Matching 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. Matching 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) $/
JavaScript Regular Validation String Method Example:
var str = "abc@126.com";
var preg =/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * * *; Match email
if (preg.test (str)) {
var msg = "Match succeeded";
} else{
var msg = "Match failed!" ";
}
Small series of 20 common expressions, I hope to help you learn.
Regular expression is a subject, it is impossible to use an article to explain the end of the theory of things online a lot of interested students can search a search, more understanding.