jQuery从锚标签内部删除多余的链接文本 - 我做错了什么?

时间:2013-04-18 22:02:16

标签: jquery regex

我放弃了 - 为什么这不起作用?我正在尝试处理我的消息的链接,它可以工作,但不是一直。 Here's the jsFiddle以及我认为问题的核心在于下面的编辑2

它运行该函数并执行除过滤器循环之外的所有操作....如果查看控制台,您可以看到它删除执行每个<a></a>标记内部文本的正则表达式,但是由于某种原因,它不会一直保持变化!我做错了什么?

JavaScript(使用jQuery):

function processTweetLinks(messagetext) {
    console.log(messagetext);
    messagetext = messagetext.replace();
    var replacePattern1, replacePattern2;
    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    messagetext = messagetext.replace(replacePattern1, '<a class="individualMessageBioWhateverLink" href="$1" target="_blank">$1</a>');
    console.log('replacePattern1: ' + messagetext);

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    messagetext = messagetext.replace(replacePattern2, '$1<a class="individualMessageBioWhateverLink" href="http://$2" target="_blank">$2</a>');
    console.log('replacePatter2: ' + messagetext);
    // Ignore this block of code, it's just for future visitors that want to link #hashtags and @usernames - it works perfectly!
    /*
         exp = /(^|\s)#(\w+)/g;
       text = text.replace(exp, "$1<a class='individualMessageBioWhateverHash' href='http://search.twitter.com/search?q=%23$2' target='_blank'>#$2</a>");
       console.log(text);

        exp = /(^|\s)@(\w+)/g;
        text = text.replace(exp, "$1<a class='individualMessageBioWhateverMention' href='http://www.twitter.com/$2' target='_blank'>@$2</a>");

        */
    console.log('before the a filter loop: ' + messagetext);
    $(messagetext).filter('a').each(function (e) {
        console.log(this.text);
        var finalexp = /(([a-zA-Z]{3,5}\:\/\/)|(www\.))/gim;
        var something = $(this).text().replace(finalexp, "");
        $(this).text(something);
        console.log(this.text);
        console.log(something);

        /*console.log('after each: ' + this);

         //cleans up stuff like http://www. and https://www. and ftp://www. (obviously not a value URI... - may want to stick to just https?:\/\/ for that reason)
         // /(([a-zA-Z]{3,5}\:\/\/)(www\.))/gim;

         //Cleans up all instances of www. 
         // /(www\.)/gim;
        });
        */
        //console.log(text);
    });
    $("#correctedmessagetext").html(messagetext);
    return messagetext;   
}

var individualMessage = "http://google.com is great, but http://www.stackoverflow.com may be my only hope. www.yahoo.com is out of the question.";
processTweetLinks(individualMessage);

HTML:

<div id="correctedmessagetext"></div>

编辑:我实际上并没有将它输出到div - 另一个函数的一部分生成一个字符串,而该字符串内部是对该函数的调用,基本上是processTweetLinks(individualMessage)。< / p>

编辑2:这是代码的简短版本 - 问题必须出在这里:

$(messagetext).filter('a').each(function(e) {
        var finalexp = /(([a-zA-Z]{3,5}\:\/\/)|(www\.))/gim;
        var something = $(this).text().replace(finalexp, "");    
        $(this).text(something);
    });
    return messagetext;

1 个答案:

答案 0 :(得分:1)

看起来你永远不会在页面上编辑它们。据我所知,你可以分解messagetext字符串(把它放到不同的变量中,然后在更新后将它们重新组合在一起),或者你可以将它输出到页面,然后在correctmessagetext上进行过滤。所以请留下:

$("#correctedmessagetext").html(messagetext);

然后将您的过滤器方法更改为另一个处理correctmessagetext的id的方法,如下所示:

function doFilter() {
     $("#correctedmessagetext").filter('a').each(function (e) {
        console.log(this.text);
        var finalexp = /(([a-zA-Z]{3,5}\:\/\/)|(www\.))/gim;
        var something = $(this).text().replace(finalexp, "");
        $(this).text(something);
        console.log(this.text);
        console.log(something);

        /*console.log('after each: ' + this);

         //cleans up stuff like http://www. and https://www. and ftp://www. (obviously not a value URI... - may want to stick to just https?:\/\/ for that reason)
         // /(([a-zA-Z]{3,5}\:\/\/)(www\.))/gim;

         //Cleans up all instances of www. 
         // /(www\.)/gim;
        });
        */
        //console.log(text);
    });
}

将messagetext输出到页面(上面的单行代码)后,再调用新方法。

修改

考虑到这一点,你已经得到了之前你正在做的答案 - 正则表达式替换。我刚刚过去了,用这种方法来了:

function messageFilter(messagetext) {
    var replacePattern = /(>)(([a-zA-Z]{3,5}\:\/\/)|(www\.))(www.)?(\w+\.\w+)(<)/g;

    messagetext = messagetext.replace(replacePattern, '$1$6$7');

    console.log(messagetext);
    return messagetext;
}

您可以在JS Fiddle

上看到它正常工作

这样你就没有做$(item).filter('a')。each(function(e)了,你可以调用方法来修复你的字符串。