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标签及其内容。