如何替换所有链接的URL如果找到sites.com on the href? 例如:使用jquery替换网页中的所有链接(href)
从这个HREF链接
http://one.sites.com/1234/12/page.html http://two.sites.com/img/dir/index.html http://any.sites.com/any/any/any.html http://*.sites.com/*/*/*.*
是
http://one.sites.com/ http://two.sites.com/ http://any.sites.com/ http://*.sites.com/
我试过使用jquery:
$("a[href*='http://one.sites.com/']").attr('href','http://one.sites.com')
但它只会取代一个链接。
我认为,如果将其与正则表达式,它会自动替换每个HREF
var isUrl=/(\()((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:ht|f)tps?:\/\/[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img;
正则表达式代码的网页有sites.com上的所有链接上面检查成功链接一样的表达:
http://one.sites.com/1/2/page.html
如何使用jQuery实现它?请帮助
你有你自己尝试新鲜事物? – putvande
你想在旅途中做到这一点吗?或者你解析一个静态文件? – amaurs
@putvande我试过使用jquery。我不知道如何使用jQuery的正则表达式 – Vimux