美文网首页
正则表达式常用的方法

正则表达式常用的方法

作者: QinRenMin | 来源:发表于2018-09-05 19:05 被阅读0次

    当初学习正则表达式没有系统的总结,当再看时知识体系不太完整,在后期学习中会带来一些麻烦,再总结,并且对于一些东西有了新的认知。

    • 创建方法
    1. 字面量创建
      let re = /正则表达式/标识(修饰符)/
      let re = /cm/g
      2.实例创建法
      let re = new RegExp(正则表达式,修饰符)
      let re = new RegExp('cm')
      两者的区别
      1实例创建法可以拼接字符
      let re = new RegExp('cm'+22)
      2字面量创建不需要转义
      let re = /\d/
      let re = new RegExp('\\d')
    • 常用方法
    1. est()
      含义:正则匹配字符串,返回布尔值
      写法:正则.test(字符串)
    (function(){
        let str = 'abc';
        let re =/a/;
        let re1 =/abc/;
        let re2 =/w/;
        console.log(re.test(str)); //true
        console.log(re1.test(str)); //true
        console.log(re2.test(str)); //false
    })();
    

    2.search()
    含义:正则匹配字符串,匹配成功返回匹配位置,否则返回-1,相当于indexOf
    用法:字符串.search(正则)
    注意:正则表达式的默认行为是区分大小写的
    如果想要不区分大小写,在后面添加i
    匹配成功则返回第一次匹配成功的值

    (function () {
        let str = 'asd12zxc2as';
        let re = /S/;
        let re1 = /S/i;
        let re2 = /aS/i;
        console.log(str.search(re)); //-1
        console.log(str.search(re1)); //1
        console.log(str.search(re2)); //0
    })();
    

    3.match()
    含义:正则匹配字符串,匹配成功返回匹配成功的数组,匹配不成功则返回null
    用法:字符串.match(正则)

    (function () {
        // let str = '123aa456qq1';
        let str = '1234561';
        let re = /\d/g;
        let re1 = /\d\d/g;
        let re2 = /\d\d\d/g;
        let re3 = /\d{1,5}/g;
        console.log(str.match(re)); //[ '1', '2', '3', '4', '5', '6' ]
        console.log(str.match(re1)); //[ '12', '34', '56' ]
        console.log(str.match(re2)); //[ '123', '456' ]
        console.log(str.match(re3)); //[ '12345', '61' ]
    })();
    

    4.exec()
    lastIndex :这个属性记录的就是下一次捕获从哪个索引开始。
    当未开始捕获时,这个值为0。
    如果当前次捕获结果为null。那么lastIndex的值会被修改为0.下次从头开始捕获。
    而且这个lastIndex属性还支持人为赋值。

    let str = 'abc123cba456aaa789';
    let reg = /\d+/g;  //此时加了标识符g
    console.log(reg.lastIndex); //未捕获
    // lastIndex : 0
    
    console.log(reg.exec(str));
    //  ["123", index: 3, input: "abc123cba456aaa789"];
    console.log(reg.lastIndex);
    // lastIndex : 6
    //reg.lastIndex = 18; 修改lastIndex值,可以更改下一次匹配的位置
    console.log(reg.exec(str)); 
    //[ '456', index: 9, input: 'abc123cba456aaa789' ]
    console.log(reg.lastIndex); //12
    
    console.log(reg.exec(str));
     //[ '789', index: 15, input: 'abc123cba456aaa789' ]
    console.log(reg.lastIndex); //18
    
    console.log(reg.exec(str)); //null
    console.log(reg.lastIndex); //0
    
    console.log(reg.exec(str)); 
    //[ '123', index: 3, input: 'abc123cba456aaa789' ]
    console.log(reg.lastIndex); //6
    

    5.replace()
    含义:正则匹配字符串,用新的字符替换匹配成功的字符,匹配不成功返回原串
    用法:字符串.replace(正则)

    (function () {
        let str = 'asd12zxc2as';
        let re = /asd/;
        let re1 = /asd/;
        let re2 = /f/;
        console.log(str.replace(re,'q')); //q12zxc2as
        console.log(str.replace(re1,'w')); //w12zxc2as
        console.log(str.replace(re2,'s')); //asd12zxc2as
    })();
    

    replace第二个参数可以接收一个回调函数,注意其参数

    (function(){
        let str = 'aasdEeAefhghhhce';
        let re = /e+/ig;
        str.replace(re,function(){
            console.log(arguments);
        })
        //{ '0': 'Ee', '1': 4, '2': 'aasdEeAefhghhhce' }
        // { '0': 'e', '1': 7, '2': 'aasdEeAefhghhhce' }
        // { '0': 'e', '1': 15, '2': 'aasdEeAefhghhhce' }
        //参数对应:匹配到的字符(串),下标,原串
        let str1 = 'aasdEeAefhghhhce';
        let re1 = /(e)\1*/ig;
        str1.replace(re1,function(){
            console.log(arguments);
        })
        // { '0': 'Ee', '1': 'Ee', '2': 4, '3': 'aasdEeAefhghhhce' }
        // { '0': 'e', '1': 'e', '2': 7, '3': 'aasdEeAefhghhhce' }
        // { '0': 'e', '1': 'e', '2': 15, '3': 'aasdEeAefhghhhce' }
        //参数对应:匹配到的字符(串),匹配到的首字符,下标,原串
    })()
    

    实例方法:test和exec
    其他都是字符串方法

    相关文章

      网友评论

          本文标题:正则表达式常用的方法

          本文链接:https://www.haomeiwen.com/subject/vsxzwftx.html