2

Is it possible to use jqueries each loop in this form to dissolve hyperlinks?

 $(document).ready(function() {
    var toExclude =['http://www.google.com', 'http://example'];
    $.each(toExclude, function(key, value) {

    $("a[href='+value+']").replaceWith(function(){
        var matches = value.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
        var domain = matches && matches[1];
        return domain;
                });
            });
        });
2
  • 3
    What do you mean by dissolve? Commented Jun 19, 2012 at 9:13
  • on match all a href should be removed and just the a href text should be returned Commented Jun 19, 2012 at 9:16

3 Answers 3

5
$("a[href='+value+']")

should be:

$("a[href='"+value+"']")

Also you probably want to change only the href property => use the .attr method:

$("a[href='"+value+"']").attr("href", function() {
    // do your replace and return the new value of the href
    ...
});
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting an error on this Syntax error, unrecognized expression
Where are you getting this error? Could you show your code? Could you show a jsfiddle.net sample illustrating your problem?
1
var toExclude = ['www.goo.com','www.dgoo.com'];

$.each(toExclude, function(i,e)
       {

           $("a[href^='"+e+"']").each(function (i,e) {$(e).replaceWith($(e).attr('href'));});
       }
​);
       ​

2 Comments

@lgt I'm supposing you want to replace each <a href='foo'>bar</a> with simply foo
yes that I would like to achieve if the a href is matching with one the values from array
0
$('a[href="'+value+'"]').attr("href", function() {
    // do your replace and return the new value of the href

A little change in quote signs.. this allows you to also do

$('a[href$="'+value+'"]').attr("href", function() {
    // do your replace and return the new value of the href

and

$('a[href^="'+value+'"]').attr("href", function() {
    // do your replace and return the new value of the href

to match links beggining and ending with value

Comments

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.