Javascript 用于检测<;a></a>;标签
我有一个正则表达式,它在我的文本字符串中的HTML锚标记之间查找文本。文本类似于Tweet,因此示例字符串为:Javascript 用于检测<;a></a>;标签,javascript,regex,Javascript,Regex,我有一个正则表达式,它在我的文本字符串中的HTML锚标记之间查找文本。文本类似于Tweet,因此示例字符串为: http://google.com is great, but http://www.stackoverflow.com may be my only hope. www.yahoo.com is out of the question. 在我的工作regexp中运行: function processTweetLinks(text) { console.log(
http://google.com is great, but http://www.stackoverflow.com may be my only hope. www.yahoo.com is out of the question.
在我的工作regexp中运行:
function processTweetLinks(text) {
console.log(text);
text = text.replace();
var replacedText, replacePattern1, replacePattern2;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
text = text.replace(replacePattern1, '<a class="individualMessageBioWhateverLink" href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
text = text.replace(replacePattern2, '$1<a class="individualMessageBioWhateverLink" href="http://$2" target="_blank">$2</a>');
console.log(text);
return text;
}
使用dom解析使其变得简单。如果要从中删除
http://
等,可以应用任何正则表达式
$('a').each(function(){
alert(this.href);
});
请参见您可能应该为此使用DOM。。。看到了吗?为什么投票被否决了?如果有什么方法可以完全避免正则表达式,请告诉我!我再也不想看到regexp了。@tylerl甚至我都不明白为什么这个被否决了。希望为否决票添加评论成为一项规则。我希望(对不起)你可以进一步扩展。现在远离计算机,但我尝试将.filter与你的.everything一起应用,但我不知道如何在何处应用我的regexp。。。有机会我会更好地解释我自己,但这让我很烦。。。
(?:<a [^>]+>.*?)(([a-zA-Z]{3,5}\:\/\/){0,1}(www\.))(?:.*?<\/a>)
`http://` and `https://` are in `$2`, and `www.` is in `$3`. How would I use this in the context of my function?
$('a').each(function(){
alert(this.href);
});