replace方法

 

 

        var str = 'hello abc abc';
        // abc替换成bbb
        let result = str.replace('abc', 'bbb')
        // let result = str.replace(/abc/g, 'bbb') // hello bbb bbb
        console.log(result); //  hello bbb abc

        // abc,保留b,b大写
        let result2 = str.replace(/a(b)(c)/g, '$1');
        console.log(result2) // hello b b
        let result1 = str.replace(/a(b)c/g, function(match, $1) {
            // console.log(args);   
            console.log(match, $1)
            return $1.toUpperCase();
        })

        console.log(result1); // hello B B    

 

posted @ 2020-11-16 21:49  没有茅台喝啤酒也行  阅读(119)  评论(0编辑  收藏  举报