Common Regular Expressions and Usage Examples of PHP and javascript

  • 2021-07-06 10:23:44
  • OfStack

In computer science, a regular expression is used to describe or match a single string of 1 series of strings that conform to a syntactic rule. In WEB development, regular expressions are usually used to detect, find and replace some strings that conform to rules, such as detecting whether the format of E-mai input by users is correct, collecting page content that conforms to rules, and so on.
Today, we use PHP and Javscript to introduce the most commonly used and practical regular expressions and their usage in WEB development. Regular expressions are a subject, and it is impossible to explain them in one article. There are many theoretical things on the Internet, and interested students can search a lot. However, you may not need to bury your head in studying regular expressions that you can't figure out. Read this article and examples to present you with common and practical regular expressions.

Usage of PHP Common Expressions:

1. Match a positive integer:/^ [1-9]\ d* $/
2. Match a 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. Match URL: ((fE33EN) {1} (tpE35EN)://) [-a-zA-Z0-9 @:% _\ +. ~ #? & //=]+)
6. Match letter beginning, 5-16 characters, alphanumeric underscore:/^ [a-zA-Z] [a-zA-Z0-9_] {4, 15} $/
7. Match numbers, letters, underscores, Chinese:/^ [\ x {4e00}-\ x {9fa5} A-Za-z0-9_] + $/u
8. Match Chinese postcode:/^ [1-9]\ d {5} $/
9. Match 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])) (([012]\ d) 3 [0-1])\ d {3} (\ dxX) $/

Example of PHP Regular Verification String Method:


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

Javascript Common Expression Usage

1. Match a positive integer:/^ [0-9] * [1-9] [0-9] * $/
2. Match a non-negative integer (positive integer +0):/^\ d + $/
3. Match Chinese:/^ [\ u4e00-\ u9fa5]/
4. Match Email:/^\ w + ([-+.]\ w +) * @\ w + ([-.]\ w +) *\.\ w + ([-.]\ w +) */
5. Match URL URL:/^ (fht) {1} (tptps):\/\/([\ w-] +\.) + [\ w-] + (\/[\ w-./? % & =]*)?/
6. Match letter beginning, 5-16 characters, alphanumeric underscore:/^ [a-zA-Z] [a-zA-Z0-9_] {4, 15} $/
7. Match numbers, letters, underscores, Chinese:/^ [\ u4e00-\ u9fa5A-Za-z0-9_] + $/
8. Match Chinese postcode:/^ [1-9]\ d {5} $/
9. Match 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])) (([012]\ d) 3 [0-1])\ d {3} (\ dxX) $/

Example of Javascript Regular Verification String Method:

 
var str = "abc@126.com";
var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; // Matching Email
if(preg.test(str)){
    var msg = " Match Successful ";
}else{
    var msg = " Match failed! ";
}
alert(msg);


Related articles: