正则总结

2018-05-20  本文已影响13人  果汁密码

正则

正则:用来操作字符串(匹配和捕获)的规则

    let reg = /\d/;
    console.log(reg.test('zsw123')); // ->true
    console.log(reg.test('zsw')); // ->false
    let reg2 = /\d+/g;
    console.log(reg2.exec('zsw123zsw456'));// ["123", index: 3, input: 
   "zsw123zsw456"]
    console.log(reg2.exec('zsw123zsw456'));// ["456", index: 9, input: 
   "zsw123zsw456"]

捕获正则有两大特点:

reg.exec() 用来捕获符合我们规则的字符串,每次只能捕获到一个,捕获到的这个是数组,数组总共包含三项:

 // 懒惰型
    let reg = /\d+/;
    console.log(reg.exec('zsw123zsw456'));// ["123", index: 3, input: "zsw123zsw456"]
    console.log(reg.exec('zsw123zsw456'));// ["123", index: 3, input: "zsw123zsw456"]
// 加全局g
 let reg = /\d+/g;
    console.log(reg.exec('zsw123zsw456'));// ["123", index: 3, input: "zsw123zsw456"]
    console.log(reg.exec('zsw123zsw456'));// ["456", index: 9, input: "zsw123zsw456"]
// 贪婪性  量词后面加?
    let reg = /\d+?/g;
    console.log(reg.exec('zsw123zsw456'));// ["1", index: 3, input: "zsw123zsw456"]
    console.log(reg.exec('zsw123zsw456'));//["2", index: 4, input: "zsw123zsw456"]

[]的总结

?的总结

    let reg=/(\d{2})(\d{2})/g;
    console.log(reg.exec('1234'));// ["1234", "12", "34", index: 0, input: "1234"]
    let reg2=/(\d{2})(?:\d{2})/g;  // ?:只匹配不捕获
    console.log(reg2.exec('1234'));// ["1234", "12", index: 0, input: "1234"]

():分组

let reg = /^(\w)\1(\w)\2$/
console.log(reg.test("zzff")) //->true
console.log(reg.test("z0f_")) // -> false

match

match实现的核心原理就是利用exec分次捕获得到的

match和exec的区别

exec

    let reg=/(\d{2})(\d{2})/g;
    console.log(reg.exec('1234'));// ["1234", "12", "34", index: 0, input: "1234"]

exec和match区别

    let reg = /(\d{2})(\d{2})/g;
    let str = 'abcd1234sad4567';
    console.log(reg.exec(str));// ["1234", "12", "34", index: 4, input: "abcd1234sad4567"]
    console.log(reg.exec(str));// ["4567", "45", "67", index: 11, input: "abcd1234sad4567"]
    console.log(str.match(reg));// ["1234", "4567"]

捕获

replace

    let reg = /(\d{2})(\d{2})/g;
    let str = 'abcd1234sad1234';
    console.log(str.replace('1234','5678')); //abcd5678sad1234
    console.log(str.replace('1234','5678')); //abcd5678sad1234
    console.log(str.replace(reg,'5678'));//abcd5678sad5678

应用:
1.有效数字 正数、负数、零、小数
"."可以出现也可以不出现,但是一旦出现,后面必须跟一位或多位数字
最开始可以有+/ -也可以没有
整数部分,一位数可以是0-9之间的一个,多位数不能以0开头
/^[+-]?(\d|([1-9]\d+))(\.\d+)?$/
2.年龄介于18-65 (18-19 20-59 60-65)
/^(1[8-9]|[2-5]\d|6[0-5])$/
3.中国标准真实姓名 2-4位汉字
/^[\u4e00-\u9fa5]{2,4}$/
4.数字转大写

    let str = '20180520';
    let ary = ['零','壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    str = str.replace(/\d/g, function () {
        return ary[arguments[0]]
    });
    console.log(str); //贰零壹捌零伍贰零

5.解析url

let str = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=obj&oq=js&rsv_pq=9560a0460000df45&rsv_t=0cf=cn&rsv_enter=1&rsv_sug3=3&rsv_sug1=3&rsv_sug7=100&rsv_sug2=0&inputT=753&rsv_sug4=752';
    let reg = /([^?=&]+)=([^?=&]+)/g;
    let obj = {};
    str.replace(reg, function () {
        obj[arguments[1]] = arguments[2]
    });
    console.log(obj);
url.png
6.get-element-by-id转成getElementById
var str = 'get-element-by-id'
str.replace(/(?:-)([a-z])/g,function(){
          return arguments[1].toUpperCase()
})
// -> "getElementById"

7.手机号中间4位替换成*

var phone='15000001234'
var res=phone.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3")
// "150****1234"

8.姓名显示最后一字其他替换成星号

var name='张老三'
var res=name.replace(/.(?=.)/g,'*')
// "**三"
// (?=)可以理解为不占匹配位,不占匹配位就不会替换
上一篇下一篇

猜你喜欢

热点阅读