美文网首页
正则表达式

正则表达式

作者: 稻草人_9ac7 | 来源:发表于2019-11-04 19:00 被阅读0次
    //========================ig以及replace的使用
    str1 = '我们是程序员,程序员是个奇怪的存在';
    var newStr1 = str1.replace(/程序员/g, '[黑客]');
    str2 = 'abAcedA'
    var newStr2 = str2.replace(/a/ig, 'P')
    
    console.log("newStr1", newStr1);
    console.log("newStr2", newStr2);
    
    
    //======================\D与[^0-9]的效果相同
    str3 = "123456"
    str4 = "123456w"
    var regexp1 = /\D/
    var regexp2 = /[^0-9]/
    
    console.log(regexp1.test(str3))
    console.log(regexp2.test(str4))
    
    //======================或的使用与使用[]的效果是一样的
    str5 = "abcde"
    var regexp3 = str5.replace(/a|c/g, '[replace]')
    var regexp4 = str5.replace(/[ac]/g, '[replace]')
    
    console.log("regexp3", regexp3)
    console.log("regexp4", regexp4)
    
    //=======================匹配任意字符
    str6 = "abc 12.3"
    var regexp5 = str6.replace(/[\d\D]/g, '*|')
    console.log("regexp5", regexp5)
    
    //======================在字符串替换中使用分组
    var str7 = '15013795539';
    var regexp7 = str7.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3');
    console.log("regexp7", regexp7);
    
    //=======================惰性匹配
    var str8 = '我们是web前端开发工程师';
    var regexp8 = /web.*/
    console.log(str8.match(regexp8))
    
    //===================
    var regexp9 = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
    
    //==========日期
    var str11 = "2019-12-29"
        //错误的实例,使用或运算的时候,需要使用()括起来,否则出错
        // var regexp10 = /^[1-2][1-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|2[1-8]|3[0-1])$/     
        // 正确的实例
    var regexp10 = /^[1-2]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|2[0-8]|3[0-1])$/
    console.log("str11====", regexp10.test(str11))
    
    //============获取数据,懒加载
    
    let obj = { "status": 0, "data": { "desc": "\u003cp\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/ccc5c52f2650eb1e53baf050b8f8474c.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/0913e57225efe1c83094014d0114fe43.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/332623886192f46875972f38ae602480.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/d0cbaabb52ee43bb3f2d3bcdd4f7eaf4.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/aaa99ec25aa3c900063f12bf0459aa70.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/b2553a8b3d5ef3af23d91c0a2bf76f5c.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/36bf84a2f561f3617a0a82693152180a.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/4c0e7a27040aadfd1191635e65397e87.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/dc83a25e0d8c4572d45ef9d051b021f9.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/5a5077995c9952f83f6a5b0a683cd716.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/806c26ccb96cf77cd69d59154c40072b.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/ab6153817e569acc5d9a14482221c2eb.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/1fc89caf7e8b5335a82b88ec8761a19b.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/484864728cd700e630db9a4a44924b58.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/e0a6d61dad787c7050c2fda20714204c.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/6715edf1f2f31587c99ef04e72a1dd59.jpg\"\u003e\u003cimg style=\"width: 750px; height: 390px;\" src=\"https://mall.s.maizuo.com/ddc94532e8d0b9d3870ee84769f6ca70.jpg\"\u003e\u003cimg style=\"width: 750px; height: 580px;\" src=\"https://mall.s.maizuo.com/4f5637e0114c538bd9c24f27fd63072d.jpg\"\u003e\u003cimg style=\"width: 750px; height: 503px;\" src=\"https://mall.s.maizuo.com/e7d6258b2df56c0a2f2d099dd5b1f981.jpg\"\u003e\u003cimg style=\"width: 750px; height: 310px;\" src=\"https://mall.s.maizuo.com/12f7dde166e2576e8ce5667dd37633ab.jpg\"\u003e\u003cbr\u003e\u003c/p\u003e" }, "msg": "success" }
    let str12 = obj.data.desc
        // 错误的实例,不能加^和$,因为这样会限制它,会出现错误
        // let regexp12 = /^https.*?(jpg|png)$/g
        //正确的实例
    let regexp12 = /https?:\/\/.*?\.(jpg|png)/g
        // let regexp12 = /https.*?(jpg|png)/g
    let arr = str12.match(regexp12)
    let imgArr = []
    arr.forEach(item => {
            let i = "![](" + item + ")"
            imgArr.push(i)
        })
        // console.log("imgArr", imgArr)
    
    //===============数组
    let arr1 = [1, 2, 3]
    console.log("数组", imgArr.join(""))
    

    相关文章

      网友评论

          本文标题:正则表达式

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