admin管理员组文章数量:1315348
I want to convert all the URLs in a javascript string to links, in this strings there are also words that begin with a hashtag #.
As of now I created two regex in cascade, one that creates html anchor tags based on urls and another that creates anchor tags for the hashtags (like in Twitter).
I am having a lot of problems trying to parse www.sitename/index.php#someAnchor into the right markup.
content = urlifyLinks(content);
content = urlifyHashtags(content);
where the two functions are as follows:
function urlifyHashtags(text) {
var hashtagRegex = /^#([a-zA-Z0-9]+)/g;
var tempText = text.replace(hashtagRegex, '<a href="index.php?keywords=$1">#$1</a>');
var hashtagRegex2 = /([^&])#([a-zA-Z0-9]+)/g;
tempText = tempText.replace(hashtagRegex2, '$1<a href="index.php?keywords=$2">#$2</a>');
return tempText;
}
function urlifyLinks(inputText) {
var replaceText, replacePattern1, replacePattern2, replacePattern3;
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText;
}
I am considering to parse the output of urlifyLinks and apply the regex to all the dom elements that are text elements on the first level, is that an ugly thing to do?
I want to convert all the URLs in a javascript string to links, in this strings there are also words that begin with a hashtag #.
As of now I created two regex in cascade, one that creates html anchor tags based on urls and another that creates anchor tags for the hashtags (like in Twitter).
I am having a lot of problems trying to parse www.sitename./index.php#someAnchor into the right markup.
content = urlifyLinks(content);
content = urlifyHashtags(content);
where the two functions are as follows:
function urlifyHashtags(text) {
var hashtagRegex = /^#([a-zA-Z0-9]+)/g;
var tempText = text.replace(hashtagRegex, '<a href="index.php?keywords=$1">#$1</a>');
var hashtagRegex2 = /([^&])#([a-zA-Z0-9]+)/g;
tempText = tempText.replace(hashtagRegex2, '$1<a href="index.php?keywords=$2">#$2</a>');
return tempText;
}
function urlifyLinks(inputText) {
var replaceText, replacePattern1, replacePattern2, replacePattern3;
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText;
}
I am considering to parse the output of urlifyLinks and apply the regex to all the dom elements that are text elements on the first level, is that an ugly thing to do?
Share Improve this question edited Jun 12, 2012 at 11:41 Luke Morgan asked Jun 12, 2012 at 11:14 Luke MorganLuke Morgan 1,9302 gold badges14 silver badges18 bronze badges 2- 1 Have you considered using github./twitter/twitter-text-js – Esailija Commented Jun 12, 2012 at 11:16
- Actually I haven't, I guess it can work, but I'd really prefer to use a simple Javascript solution without using an external library just for that functionality – Luke Morgan Commented Jun 12, 2012 at 11:22
1 Answer
Reset to default 11You can avoid this problem by using a single regex with a callback function replacement.
For example:
function linkify(str){
// order matters
var re = [
"\\b((?:https?|ftp)://[^\\s\"'<>]+)\\b",
"\\b(www\\.[^\\s\"'<>]+)\\b",
"\\b(\\w[\\w.+-]*@[\\w.-]+\\.[a-z]{2,6})\\b",
"#([a-z0-9]+)"];
re = new RegExp(re.join('|'), "gi");
return str.replace(re, function(match, url, www, mail, twitler){
if(url)
return "<a href=\"" + url + "\">" + url + "</a>";
if(www)
return "<a href=\"http://" + www + "\">" + www + "</a>";
if(mail)
return "<a href=\"mailto:" + mail + "\">" + mail + "</a>";
if(twitler)
return "<a href=\"foo?bar=" + twitler + "\">#" + twitler + "</a>";
// shouldnt get here, but just in case
return match;
});
}
本文标签: javascriptRegex to transform hashtag in a link without breaking existing HTML codeStack Overflow
版权声明:本文标题:javascript - Regex to transform hashtag in a link without breaking existing HTML code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741976564a2408157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论