2

You may have see various sites where URLs are encoded by an advertisement redirection service. I want to write a personal javascript snippet to remove this spam.

Mostly, the HTML will look like this:

<a target="_blank" href="http://ads.example.com/246619/http://www.example.net/path/I/want">SteamTable App</a>

The problem is that the URL path http://ads.example.com/246619/ is different for each link.

My attempt:

$(function() {
    var x = $('div.content.clearfix a').get();
    //alert(x.length);
    for (i = 0; i < x.length; i++) {
        var Href = $(x[i]).attr('href');

        Href = Href.replace("http://ads.example.com/.*?/", "");
        alert(Href);  //success or not
        $(x[i]).attr('href', Href);
    }
});

Demo on JS Fiddle.

I don't know why this isn't working.

0

1 Answer 1

3

Remove the quotes around your regex and use regex delimiters

Href = Href.replace(/http:\/\/url.sh\/.*?\//, "");
Sign up to request clarification or add additional context in comments.

2 Comments

what is the difference b/w my method & this one:Href=Href.replace( /http\:\/\/url.sh\/[^\/]+\//i, "") ;
The [^\/]+ is a substitution for the lazy matching of all characters .*?. Its matching any character, but the slash, so the result should be the same. This regex is ending with the i modifier its making the regex matching case independend, i.e. "a" will also match "A", but since there are not letters in the regex its useless here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.