码迷,mamicode.com
首页 > 其他好文 > 详细

正则常用

时间:2018-05-27 22:15:00      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:inpu   表达式   必须   空白   数字   正整数   a*   console   and   

正则表达式
let reg = /s/;//匹配非空白字符
let str =  "a " ;
console.log(reg.test(str));
let reg1 = /d/;//匹配0-9
console.log(reg.test(str));
let reg2 = /D/;//匹配非(0-9)
console.log(reg.test(str));

let reg3 = /[\d\D]/;//匹配全部
let str1 = "123";
console.log(reg3.test(str1));
let reg4 = /[\w\W]/;//匹配全部
let str2 = "sadaaf";
console.log(reg4.test(str2));

let reg5 = /\w/;//匹配数字字母下划线
console.log(reg5.test(str2));
let reg6 = /\W/;//匹配非数字字母下划线
console.log(reg6.test(str2));
let reg7 = /\b/;
console.log(reg7.test(str2));
let reg8 = /[a-zA-Z]/;//全部都是字母
console.log(reg8.test(str2));
let reg9 = /^0-9/;//全部都是数字
console.log(reg9.test(str2));

let str4 = "退出";//匹配汉字
let reg11 = /^[\u4E00-\u9FA5]{2,4}$/;
console.log(reg11.test(str4));
let str5 = ‘ababab‘;
let reg12 = /b{3}/;//匹配3个b
console.log(reg12.test(str5));
let reg13 = /a*/;//匹配至少一个a
console.log(reg13.test(str5));
let reg14 = /a\?/;//匹配a?
console.log(reg14.test(str5));

匹配手机号
let reg1 = /^1[3584]\d{9}$/
let str1 = "13234567823";
console.log(reg1.test(str1));

正整数和0
以0开头
let reg = /^\d+$/;
let str = "01";
let str7 = "1234";
console.log(reg.test(str));
console.log(reg.test(str7));

不以 0 开头
let reg2 = /^([1-9]\d*)|(0)$/;
let str2 = "11";
let str8 = "1";
let str3 = "-12";
let str4 = "0";
let str5 = "123";
let str6 = "0123456";
console.log(reg2.test(str2));
console.log(reg2.test(str3));
console.log(reg2.test(str4));
console.log(reg2.test(str5));
console.log(reg2.test(str6));

小数
let reg3 = /^\d+\.\d+$/;
console.log(reg3.test("0.2"));
console.log(reg3.test("123.2"));
console.log(reg3.test("123.0"));
console.log(reg3.test("03.4"));
console.log(reg3.test("0123.2"));
小数 
let reg4 = /^(0|[1-9]\d*)\.\d*[1-9]$/;
console.log(reg4.test("0.00"));
console.log(reg4.test("02.1"));
console.log(reg4.test("0.9"));
console.log(reg4.test("123.90"));
console.log(reg4.test("10.9"));
整数
let reg4 = /^-?\d+$/;
console.log(reg4.test("0.9"));
console.log(reg4.test("0"));
console.log(reg4.test("1"));
console.log(reg4.test("-10"));
console.log(reg4.test("01"));

整数
let reg5 = /^-?(0|([1-9]\d*))$/
console.log(reg5.test("0"));
console.log(reg5.test("01"));
console.log(reg5.test("193"));

6-20位密码,必须以字母开头,数字字母下划线都可以
let reg = /^[A-Za-z]\w{5,19}$/
console.log(reg.test("0dasdfssghsfjgdghjh"));
console.log(reg.test("a11111"));
console.log(reg.test("a_1111"));
console.log(reg.test("a1111"));
console.log(reg.test("a11111aaaaaaaaaaaaaa"));
console.log(reg.test("a11111aaaaaaaaaaaaaa_"));

邮箱
let reg = /^\w+@\w+(\.\w+){1,}$/
let reg = /^\w+@\w+(\.\w+)+$/
console.log(reg.test("16d@qq.com"));
console.log(reg.test("16d@163.com"));
console.log(reg.test("86534@qq.com.cn"));
console.log(reg.test("123456789@sina.com")); 

let str = "Mr Blue has a blue car and blue house";
let reg = /blue/;
// console.log(str.search(reg));//找到第一个blue的下标
let reg1 = /blue/i;//i表示不区分大小写
console.log(str.search(reg1));//找到第一个不区分大小写的blue的下标
let reg2 = /blue/gi;//i表示不区分大小写,g表示全局查找
console.log(str.search(reg2));//全局查找,找到第一个不区分大小写的blue的下标
let reg3 = /blue/gi;//
console.log(str.match(reg3));//全局查找,不区分大小写的所有blue

let str = "Blue has a blue car and blue house";
let reg = /blue/;
console.log(str.search(reg));//查找下标
console.log(str.match(reg));//查找不仅仅是下标,跟exec()类似,包括字符串,下标index以及来源input
console.log(reg.exec(str));//查找字符串,下标index,来源input
console.log(str.replace(reg,‘red‘));//替换

 

正则常用

标签:inpu   表达式   必须   空白   数字   正整数   a*   console   and   

原文地址:https://www.cnblogs.com/cj-18/p/9097542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!