0

URL、Twitterのユーザー名、ハッシュタグ、16進数の色の4つを検出する複数の高度な表現を開発しています。たとえば、4 つの異なるハッシュタグ、URL、または色など、同じものを配置するとうまく機能します。しかし、たとえばハッシュタグと URL などの別のものを入れると、ハッシュタグは消えます。ここで、RegularExp を確認できます。

var RegularExp = {
    twitter: /(^|[,\s])@(\w{1,15})/g,
    color: /(^|[,\s])#((?:[a-fA-F0-9]){3}|(?:[a-fA-F0-9]){6})/g,
    hashtag: /(^|[,\s])#(\w{1,15})/g,
    url: /(^|\s)(((http(s)?:\/\/|ftp(s)?:\/\/)?([a-zA-Z0-9_-]+\.)?(?:[a-zA-Z0-9]+)(?:\.[a-z]{2,4}){1,2})(\/.*)*)/g
},
Replacer = {
    twitter: "$1<a rel='nofollow' target='_blank' href='http://twitter.com/$2'>@$2</a>",
    color: "$1<span class='hex-color' style='background-color:#$2 !important'>#$2</span>",
    hashtag: "$1<a rel='nofollow' target='_blank' href='http://twitter.com/search?q=%23$2&src=hash'>#$2</a>",
    url: "$1<a rel='nofollow' target='_blank' href='$2'>$2</a>"
};

ここで試してみることができます: http://jsfiddle.net/esa_u7/KrTMW/ 最初のテキストエリアに「#fff, #000, #00f」とスラッシュなしで書き込んで (動作していることがわかります)、Twitter のユーザー名を追加します。 @例のように。なぜすべてのハッシュタグが消えるのですか?

4

2 に答える 2

0

エラーが見つかりました。私は次のようなループを作っていました:

$.each(RegularExp, function (name, value) {
    if (RegularExp[name].test(self.nodeValue)) {
        $(self).replaceWith(self.nodeValue.replace(RegularExp[name], Replacer[name]));
    }
    $output.find("span").each(function () {
        if ($(this).hasClass("hex-color")) {
            $(this).contrastColor("color", "background-color");
        }
    });
});

そして、それを行う正しい方法は次のとおりです。

var indice = 1,
    texto = this.nodeValue;
$.each(RegularExp, function (name, value) {
    if (RegularExp[name].test(self.nodeValue)) {
        texto = texto.replace(RegularExp[name], Replacer[name])
    }
    if(indice == Object.keys(RegularExp).length){
        $(self).replaceWith(texto);
    }
    $output.find("span").each(function () {
        if ($(this).hasClass("hex-color")) {
            $(this).contrastColor("color", "background-color");
        }
    });
    indice++;
});
于 2013-08-20T15:45:00.050 に答える
0

ハッシュタグは正規表現区切り文字として使用でき、エスケープする必要があります\#

于 2013-08-19T20:06:30.107 に答える