常用正则表达式(PHP+JS)

我们在开发中经常会用到正则匹配来做一些简单的验证工作,正则表达式这东西一个人一个写法,临着要用再去自己想和写实在浪费时间,所以在这里罗列一些常用的正则表达式,在使用到的时候直接使用就好。话不多说上干货。

1.匹配邮箱

PHP版:

$str  =  '';

$isMatch  =  preg_match('/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/',  $str,  $match);

var_dump($isMatch,  $match);

JS版:

var pattern = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/,str = '';

console.log(pattern.test(str));

2.匹配手机号

PHP版:

$str  =  '';

$isMatch  =  preg_match('/(13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7}/',  $str,  $match);

var_dump($isMatch,  $match);

JS版:

var pattern = /(13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7}/,
    str = '';

console.log(pattern.test(str));

3.匹配中文

PHP版:

$str  =  '';

$isMatch  =  preg_match('/[\u4e00-\u9fa5]/',  $str,  $match);

var_dump($isMatch,  $match);

JS版:

var pattern = /[\u4e00-\u9fa5]/,str = '';

console.log(pattern.test(str));

4.匹配时间(格式为:时:分:秒)

PHP版本:

$str  =  '';

$isMatch  =  preg_match('/([01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d/',  $str,  $match);

var_dump($isMatch,  $match);

JS版:

var pattern = /([01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d/,str = '';

console.log(pattern.test(str));

5.匹配IPV4地址

PHP版:

$str  =  '';

$isMatch  =  preg_match('/(\d+)\.(\d+)\.(\d+)\.(\d+)/',  $str,  $match);

var_dump($isMatch,  $match);

JS版:

var  pattern  =  /(\d+)\.(\d+)\.(\d+)\.(\d+)/,str  =  '';

console.log(pattern.test(str));

6.匹配身份证

PHP版:

$str  =  '';

$isMatch  =  preg_match('/\d{15}|\d{17}[0-9Xx]/',  $str,  $match);

var_dump($isMatch,  $match);

JS版:

var  pattern  =  /\d{15}|\d{17}[0-9Xx]/,str  =  '';

console.log(pattern.test(str));

7.匹配日期(格式:年-月-日)

PHP版:

$str = '';
$isMatch = preg_match('/((((1[6-9]|[2-9]\d)\d{2})-(1[02]|0?[13578])-([12]\d|3[01]|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})-(1
    [012]|0?[13456789])-([12]\d|30|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(1\d|2[0-8]|0?[1-9]))|(((1[6-9]|[2-9]\d)(0[48]|
    [2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))/', $str, $match);
var_dump($isMatch, $match);

JS版:

var pattern = /((((1[6-9]|[2-9]\d)\d{2})-(1[02]|0?[13578])-([12]\d|3[01]|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})-(1[012]|0?[13456789])-([12]\d|30|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(1\d|2[0-8]|0?[1-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))/,
    str = '';
console.log(pattern.test(str));

8.匹配日期(格式:年/月/日)

PHP版:

$str = '';
$isMatch = preg_match('/((((1[6-9]|[2-9]\d)\d{2})\/(1[02]|0?[13578])\/([12]\d|3[01]|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})\/
    (1[012]|0?[13456789])\/([12]\d|30|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(1\d|2[0-8]|0?[1-9]))|(((1[6-9]|[2-9]\d)(0[
    48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))/', $str, $match);
var_dump($isMatch, $match);

JS版:

var pattern = /((((1[6-9]|[2-9]\d)\d{2})\/(1[02]|0?[13578])\/([12]\d|3[01]|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})\/(1[012]|0?[13456789])\/([12]\d|30|0?[1-9]))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(1\d|2[0-8]|0?[1-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))/,
    str = '';
console.log(pattern.test(str));

9.匹配正整数

PHP版:

$str = '';
$isMatch = preg_match('/[1-9]\d*/', $str, $match);
var_dump($isMatch, $match);

JS版:

var pattern = /[1-9]\d*/,
    str = '';
console.log(pattern.test(str));

10.匹配负整数

PHP版:

$str = '';
$isMatch = preg_match('/-[1-9]\d*/', $str, $match);
var_dump($isMatch, $match);

JS版:

var pattern = /-[1-9]\d*/,
    str = '';
console.log(pattern.test(str));

11.电驴链接(似乎看到了老司机的微笑)

PHP版:

$str = '';
$isMatch= preg_match('/ed2k:\/\/\|file\|([^\|]+?)\|(\d+?)\|([0-9a-zA-Z]{32})\|((?:\/\|sources,([^\s\|]+?)\||h=([0-9a-zA-Z]{32})\||s=([^\s\|]+?)\||p=([^\s\|]+?)\|)*)\//', $str, $match);
var_dump($isMatch, $match);

JS版:

var pattern = /ed2k:\/\/\|file\|([^\|]+?)\|(\d+?)\|([0-9a-zA-Z]{32})\|((?:\/\|sources,([^\s\|]+?)\||h=([0-9a-zA-Z]{32})\||s=([^\s\|]+?)\||p=([^\s\|]+?)\|)*)\//,
    str = '';
console.log(pattern.test(str));

本文来自清渭技术小站

你可能感兴趣的:(常用正则表达式(PHP+JS))