zoukankan      html  css  js  c++  java
  • ☀【正则表达式】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>
  • 相关阅读:
    HTML元素解释
    Java命名规范
    HDU 1058 Humble Numbers(DP,数)
    HDU 2845 Beans(DP,最大不连续和)
    HDU 2830 Matrix Swapping II (DP,最大全1矩阵)
    HDU 2870 Largest Submatrix(DP)
    HDU 1421 搬寝室(DP)
    HDU 2844 Coins (组合背包)
    HDU 2577 How to Type(模拟)
    HDU 2159 FATE(二维完全背包)
  • 原文地址:https://www.cnblogs.com/jzm17173/p/2560519.html
Copyright © 2011-2022 走看看