zoukankan      html  css  js  c++  java
  • 记录三条重要的正则表达式

    1. <scripts+S+comment.js[sS]*?</script>
    结果可以匹配出包含comment.js的script标签的内容,比如:
    输入:

        ...
        <script>
        document.documentElement.className=document.documentElement.className.replace(/no-js/,'js');
        if(window.Shopify&&window.Shopify.designMode)document.documentElement.className+=' in-theme-editor';
        if(('ontouchstart' in window)||window.DocumentTouch&&document instanceof DocumentTouch)document.documentElement.className=document.documentElement.className.replace(/no-touch/,'has-touch');
        </script>

        <link href="//cdn.shopify.com/s/files/1/0056/2370/9799/t/38/assets/theme.scss.css?10509" rel="stylesheet" type="text/css" media="all" />
        <script src="https://alireviews-cdn.fireapps.vn/js/frontend/comment.js" defer="defer"></script>
        <link rel="stylesheet" href="https://alireviews-cdn.fireapps.vn/css/frontend/vendor.css">
        <link rel="stylesheet" href="https://alireviews-cdn.fireapps.vn/css/frontend/grid.css">
        ...

      使用:

        Matcher matcher = Pattern.compile("<script\s+\S+comment.js[\s\S]*?</script>").matcher(text);
        if (matcher.find()) {
          String comment = matcher.group(0);
        }

      输出:<script src="https://alireviews-cdn.fireapps.vn/js/frontend/comment.js" defer="defer"></script> 

      解释:s+表示一个或者多个空格换行符,S+表示一个或者多个非空格或者非换行符,[]表示一个组,×表示一个或者多个,关键是在于?,?是非贪婪匹配,即只有匹配到一个</script>结束

    2. src[S]*empire.js[sS]*?</script>
    输入:

        ...
        </svg>
        </button>
        <div class="modal-content" data-modal-content></div>
        </div>
        </div>
        <script
        src="//cdn.shopify.com/s/files/1/0056/2370/9799/t/38/assets/empire.js?10509"
        data-scripts
        data-shopify-api-url="//cdn.shopify.com/s/assets/themes_support/api.jquery-e94e010e92e659b566dbc436fdfe5242764380e00398907a14955ba301a4749f.js"
        data-shopify-currencies="//cdn.shopify.com/s/javascripts/currencies.js"
        data-shopify-countries="/services/javascripts/countries.js"
        data-shopify-common="//cdn.shopify.com/s/assets/themes_support/shopify_common-8ea6ac3faf357236a97f5de749df4da6e8436ca107bc3a4ee805cbf08bc47392.js"
        data-shopify-cart="//cdn.shopify.com/s/files/1/0056/2370/9799/t/38/assets/jquery.cart.js?10509">
        </script>
        ...

      使用:

        Matcher matcher = Pattern.compile("src[\S]*empire.js[\s\S]*?</script>").matcher(text);
        if (matcher.find()) {
          String empire = matcher.group(0);
        }

      输出:

        src="//cdn.shopify.com/s/files/1/0056/2370/9799/t/38/assets/empire.js?10509"
        data-scripts
        data-shopify-api-url="//cdn.shopify.com/s/assets/themes_support/api.jquery-e94e010e92e659b566dbc436fdfe5242764380e00398907a14955ba301a4749f.js"
        data-shopify-currencies="//cdn.shopify.com/s/javascripts/currencies.js"
        data-shopify-countries="/services/javascripts/countries.js"
        data-shopify-common="//cdn.shopify.com/s/assets/themes_support/shopify_common-8ea6ac3faf357236a97f5de749df4da6e8436ca107bc3a4ee805cbf08bc47392.js"
        data-shopify-cart="//cdn.shopify.com/s/files/1/0056/2370/9799/t/38/assets/jquery.cart.js?10509">
        </script>

    3. <script(s+.*)cdn.shopify.com(S*)assets([sS]*?)</script>
    匹配结果:<script src="/cdn.shopify.com/.../.../assets/..."></script>,就是匹配含有cdn.shopify.com、assets关键字的script标签及其内容。
  • 相关阅读:
    浅析如何让 (a === 1 && a === 2 && a === 3) 返回 true
    浅析单点登录的三种实现方式
    浅析瀑布流布局原理及实现方式
    浅析Java中三目运算符可能产生的坑
    【转】IO
    [转]gomonkey学习
    从sha1的计算例子,理解计算机的数据表示?16进制输出?二进制输出??
    [转]Golang第三方包应该如何安装--在线和离线
    【转】goconvey使用
    go import 时 点号 和下划线的区别
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/10894435.html
Copyright © 2011-2022 走看看