Javascript中的高级正则表达式

Javascript中的高级正则表达式,javascript,regex,url,hashtag,zepto,Javascript,Regex,Url,Hashtag,Zepto,我正在开发多个高级表达式,可以检测4种东西:URL、twitter用户名、hashtags和十六进制颜色。如果你放了相同的东西,例如,4个不同的标签、URL或颜色,效果会很好。但是当你放置一个标签和一个不同的东西,比如url,标签就会消失。 在这里您可以看到RegularExp: var RegularExp = { twitter: /(^|[,\s])@(\w{1,15})/g, color: /(^|[,\s])#((?:[a-fA-F0-9]){3}|(?:[a-fA-F

我正在开发多个高级表达式,可以检测4种东西:URL、twitter用户名、hashtags和十六进制颜色。如果你放了相同的东西,例如,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>"
};
var RegularExp={
推特:/(^ |[,\s])@(\w{1,15})/g,
颜色:/(^ |[,\s])#((?:[a-fA-F0-9]){3}|(?:[a-fA-F0-9]){6})/g,
hashtag:/(^ |[,\s])#(\w{1,15})/g,
网址:/(^ |\s)((http(s):\/\/\/\/\\/\/\/)([a-zA-Z0-9\-]+\)(?:[a-zA-Z0-9]+)(?:\.[a-z]{2,4}{1,2})(\/.*)/g
},
替换者={
推特:“1美元”,
颜色:“$1#$2”,
标签:“$1”,
网址:“$1”
};
您可以在这里尝试: 在第一个文本区域中写下:“fff,#000,#00f”,不带斜杠(正如您看到的那样),然后添加一个twitter用户名,如@example。
为什么所有的hashtag都消失了。

hashtag可以用作regex分隔符,需要转义
\\35;
我发现了错误。我在做一个循环,就像:

$.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++;
});

好吧,那不是真的。在JavaScript中肯定不是真的。JSFIDLE似乎对我来说什么都没有做。它将文本从顶部文本区域复制到另一个区域,但什么也没发生。@Pointy你说得对,原因是JSFIDLE加载了另一个版本的Zepto库;控制台说函数contents()不存在,但现在我已经更新了它。现在,你可以再试一次。