常用18个正则表达式

2024年07月30日 建站教程

1、中文字符

var pattern = /[\u4e00-\u9fa5]/;
var str = "建站教程";
console.log(pattern.test(str));

2、双字节字符

var pattern = /[^\x00-\xff]/;
var str = "建站教程ipkd";
console.log(pattern.test(str));

3、空白行

var pattern = /\s/;
var str = "\n";
console.log(pattern.test(str));

4、Email地址

var pattern = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
var str = "ipkd@163.com";
console.log(pattern.test(str));

5、网址URL

var pattern = /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/;
var str = "http://www.lmcjl.com";
console.log(pattern.test(str));

6、手机(国内)

var pattern = /0?(13|14|15|18)[0-9]{9}/;
var str = "13688888888";
console.log(pattern.test(str));

7、电话号码(国内)

var pattern = /[0-9-()()]{7,18}/;
var str = "(0516)-88888888";
console.log(pattern.test(str));

8、负浮点数

var pattern = /-([1-9]\d*.\d*|0.\d*[1-9]\d*)/;
var str = "-3.1415926";
console.log(pattern.test(str));

9、匹配整数

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

10、正浮点数

var pattern = /[1-9]\d*.\d*|0.\d*[1-9]\d*/;
var str = "3.1415926";
console.log(pattern.test(str));

11、腾讯QQ号

var pattern = /[1-9]([0-9]{5,11})/;
var str = "12345678";
console.log(pattern.test(str));

12、邮政编码

var pattern = /\d{6}/;
var str = "221000";
console.log(pattern.test(str));

13、IP格式

var pattern = /(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)/;
var str = "192.168.225.255";
console.log(pattern.test(str));

14、身份证号

var pattern = /\d{17}[\d|x]|\d{15}/;
var str = "320102199002102937";
console.log(pattern.test(str));

15、格式日期

var pattern = /\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}/;
var str = "1988-05-20";
console.log(pattern.test(str));

16、正整数

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

17、负整数

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

18、用户名

var pattern = /[A-Za-z0-9_\-\u4e00-\u9fa5]+/;
var str = "建站教程-ipkd";
console.log(pattern.test(str));

本文链接:http://so.lmcjl.com/news/9526/

展开阅读全文
相关内容