[英]How can I rewrite links with JavaScript or jQuery and a regular expression?
想要修改Yahoo Answers链接以删除一些部分,只保存qid
并用answer
替换index
。
所以这:
http://answers.yahoo.com/question/index;_ylt=AhT5ZZwbMiGWdQZDSxD1ML305nNG;_ylv=3?qid=20121004094847AAjekoj
成为这个:
http://answers.yahoo.com/question/answer?qid=20121004094847AAjekoj
我知道有办法用.htaccess
重写链接,但在这种情况下,它需要是一个Greasemonkey脚本,因为任务将在我访问的网站上完成,而不是我的网站。
你需要的模式是这样的:
/(http:\/\/answers.yahoo.com\/questions\/)index.*(?qid=.*)$/i
而你用这个代替它:
/$1answer$2/
不过我对Greasemonkey并不是很了解,所以我不能给你更多。 希望有更多Greasemonkey知识的人出现并提供更好的答案。
总的来说,这应该这样做( 完整的GM脚本 ):
// ==UserScript==
// @name _Replace yahoo-answers links
// @include http://answers.yahoo.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change introduced
in GM 1.0. It restores the sandbox.
*/
var targLinks = $("a[href*='question/index']");
targLinks.each ( function () {
if (/\bqid=\w+/i.test (this.href) ) {
var newHref = this.href
var newPath = this.pathname.replace (/\/question\/index.+$/, "/question/answer");
var newURL = this.protocol + "//"
+ this.host
+ newPath
+ this.search
+ this.hash
;
this.href = newURL;
}
} );
更换
/(http:\/\/answers\.yahoo\.com\/question)\/index.+[?&]qid=([^&#]+)/g
同
"$1/answer?quid=$2"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.