GitHub 博客园 Nanakon

☀【正则表达式】replace

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
'sort,reverse,pop,shift'.replace(/\w+/g, function(method) {
    setTimeout(function() {
        if (method === 'sort') {
            array[method](function(a, b) {
                return a - b
            })
        } else {
            array[method]()
        }
        console.log(array)
    }, 1000)
})

 

 

js过滤HTML标签以及 

 

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <script>
        var str;

        // {擦estTitle}
        console.log('{destTitle}'.replace(/[a-zA-Z]/, ''));

        // {擦}
        console.log('{destTitle}'.replace(/([a-zA-Z])+/, ''));

        //
        console.log('{destTitle}'.replace(/\{(([a-zA-Z])+)\}/, ''));

        // {destTitle} destTitle e 0
        '{destTitle}'.replace(/\{(([a-zA-Z])+)\}/ig, function(a, b, c, d) {
            console.log(a, b, c, d);
        });

        // {destTitle}
        '{destTitle}'.replace(/\{(([a-zA-Z])+)\}/ig, function(a) {
            console.log(a);
        });

        // destTitle
        '{destTitle}'.replace(/([a-zA-Z])+/ig, function(a) {
            console.log(a);
        });

        // d e s t T i t l e 一个个匹配回来
        str = '{destTitle}'.replace(/[a-zA-Z]/ig, function(a) {
            return 2;
        });
        console.log(str)

        '{destTitle}'.replace(/[a-zA-Z]/ig, function(a, b) {
            console.log(a, b);
        });
    </script>   
</body>
</html>

 

 

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <script>
        var uid = 46690773;
        var str = 'http://video.baomihua.com/37232491&uid={}';
        console.log(str.replace(/uid=\{\}/g, 'uid='+ uid));

        var str = 'http://video.baomihua.com/hot/37232491';
        console.log(str.replace(/\/hot\//g, '/live/'));

        /*
        * 转义序列
        * \0 NUL字符\u0000
        * \b 退格符\u0008
        * \t 水平制表符\u0009
        * \n 换行符\u000A
        * \v 垂直制表符\u000B
        * \f 换页符\u000C
        * \r 回车符\u000D
        * \" 双引号\u0022
        * \' 撇号或单引号\u0027
        * \\ 反斜线符\u005C
        * \xXX 由两位十六进制数值XX指定的Latin-1字符
        * \uXXXX 由四位十六进制数XXXX指定的Unicode字符
        * \XXX 由一位到三位八进制指定的Latin-1字符。ECMAScript v3不支持,不要使用这种转义序列
        */

        // 回车
        var str = 'nnn\nnnn';
        console.log(str);
        console.log(str.replace(/\n/gm, ''));

        // 换行
        str = 'rrr\rrrr';
        console.log(str);
        console.log(str.replace(/\r/gm, ''));
    </script>
</body>
</html>

 

posted on 2012-06-24 22:18  jzm17173  阅读(198)  评论(0)    收藏  举报

导航

轻音