如何根据DIV标记中的关键字删除所有网址

时间:2017-07-02 05:59:16

标签: php jquery

如何删除从特定关键字过滤的DIV标记中的特定网址。假设我有这样的文字:

<div class="target">
https://my-site.com/test1_good.html
https://my-site.com/test1_fake.html
https://my-site.com/test2_good.html
https://my-site.com/test2_fake.html
https://my-site.com/test3_good.html
https://my-site.com/test3_fake.html
</div>

我希望所有包含关键字"_good.html"的网址都显示为:

<div class="target">
https://my-site.com/test1_good.html
https://my-site.com/test2_good.html
https://my-site.com/test3_good.html
</div>

可以这样做吗?无论是使用Javascript,jQuery还是PHP都无关紧要。 谢谢..

1 个答案:

答案 0 :(得分:0)

这取决于您最初输出网址的方式。如果您使用PHP并在循环中输出每个PHP,则可以使用preg_match(),否则您可以使用匹配使用JavaScript,如下所示:

<script>
$(document).ready(function() {
    // Fetch the urls
    var getCont =   $('.target').text();
    // Create storage
    var getGood =   [];
    // Loop the urls
    $.each(getCont.split('\n'),function(k,v) {
        // If contains the string, save to storage
        if(v.match(/_good/i))
            getGood.push(v);
    });
    // Tell us about it...
    console.log(getGood);
});
</script>

在控制台中,为您提供:

[
 "https://my-site.com/test1_good.html",
 "https://my-site.com/test2_good.html",
 "https://my-site.com/test3_good.html"
]