【问题标题】:how to remove some part till the end in all href links on page如何在页面上的所有href链接中删除某些部分直到最后
【发布时间】:2014-12-13 04:57:47
【问题描述】:

我在同一页面上有太多这样的链接,我想删除从指定字符到该 URL 末尾的所有内容。知道我的页面中有太多这样的链接

$amp;rs 或 &rs 在实际 url 之前的所有链接中

$string='<td><a href="https://mywebsite.com/book-234-god-is-one&amp;rs=T&amp;pi=DLaLVKLiMIagugSr9YFo&amp;fed=0CBUQ9QEwAA&amp;sdg=AFQjCNHwYLPqh3dXrKGaRucO6diyAt-hjQ"><img src="image.jpg"></a></td>';

                                           or 

$string='<td><a href="https://mywebsite.com/book-234-god-is-one&rs=T&pi=DLaLVKLiMIagugSr9YFo&fed=0CBUQ9QEwAA&sdg=AFQjCNHwYLPqh3dXrKGaRucO6diyAt-hjQ"><img src="image.jpg"></a></td>';

我想要所有这样的链接:

<a href="https://mywebsite.com/book-234-god-is-one"><img src="image.jpg"></a>

【问题讨论】:

    标签: php html regex url


    【解决方案1】:

    只需对所有a 标签运行each 循环并修改它们的href 属性:

    $('a').each(function() {
        var href = $(this).attr('href');
        var newHref = href.replace(/&(amp|rs).*/, '');
        $(this).attr('href', newHref);
    });
    

    JSFiddle Demo

    【讨论】:

    • 感谢大家的支持,但这对我来说效果最好,我不想在服务器上增加额外的负载
    【解决方案2】:

    你可以使用正则表达式

    /&(amp|rs)[^"]+/
    

    并替换为''

    DEMO

    【讨论】:

    • @royleewheels 很高兴为您提供帮助 :)
    【解决方案3】:

    使用下面的正则表达式,然后用空字符串替换匹配项。

    &(?:amp;)?rs[^"]*
    

    或

    使用此正则表达式,然后将匹配替换为 \1

    (<a href=".*?)&(?:amp;)?rs[^"]*
    

    DEMO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 2018-06-24
      相关资源
      最近更新 更多