Skip to content Skip to sidebar Skip to footer

How Can I Rewrite Links With Javascript Or Jquery And A Regular Expression?

Looking to modify Yahoo Answers links to remove some parts, saving just the qid and replacing index with answer. So this: http://answers.yahoo.com/question/index;_ylt=AhT5ZZwbMiGWd

Solution 1:

The pattern you need here would be this:

/(http:\/\/answers.yahoo.com\/questions\/)index.*(?qid=.*)$/i

And you'd replace it with this:

/$1answer$2/

I don't really know a lot about Greasemonkey, though, so I can't give you more than this. Hopefully someone with more knowledge of Greasemonkey comes along and provides a better answer.

Solution 2:

In General, this should do it (Complete GM script):

// ==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.hrefvar newPath = this.pathname.replace (/\/question\/index.+$/, "/question/answer");
        var newURL  = this.protocol + "//"
                    + this.host
                    + newPath
                    + this.search
                    + this.hash
                    ;
        this.href   = newURL;
    }
} );

Solution 3:

Replace

/(http:\/\/answers\.yahoo\.com\/question)\/index.+[?&]qid=([^&#]+)/g

with

"$1/answer?quid=$2"

Post a Comment for "How Can I Rewrite Links With Javascript Or Jquery And A Regular Expression?"