我有一个字符串(推文),如此:
var str = "blah blah blah blah http://example.com blah blah #something blah blah https://example.com blah blah @person"
我想挑选出应该是链接的所有内容并用链接标记包装它。需要在字符串中找到以下4件事:
@
#
http://
https://
所以我想的是它有点像这样:
str.replace(regexForHashtag, '<a href="' + linkText + '">" + linkText + "</a>");
答案 0 :(得分:1)
基于Parsing Twitter Usernames, Hashtags and URLs with JavaScript:
String.prototype.parseURL = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
return url.link(url);
});
};
String.prototype.parseUsername = function() {
return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
var username = u.replace("@","");
return u.link("http://twitter.com/"+username);
});
};
String.prototype.parseHashtag = function() {
return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
var tag = t.replace("#","%23");
return t.link("http://search.twitter.com/search?q="+tag);
});
};
String.prototype.parseTweet = function() {
return this.parseURL().parseUsername().parseHashtag();
};
可以这样称呼:
var str = "blah blah blah blah http://example.com blah blah #something blah blah https://example.com blah blah @person"
str.parseTweet();
// or:
// str.parseURL
// str.parseUsername
// str.parseHashtag
请参阅this jsBin
答案 1 :(得分:0)
我使用以下正则表达式:
/(?:^|\s)((?:@|#|https?:\/\/)\S+)/
你需要的词在第1组。
答案 2 :(得分:0)
您可以简单地使用捕获组:
a = "blah blah blah blah http://example.com blah blah #something blah blah https://example.com blah blah @person";
a = a.replace(/(^|\s)(((https?:\/\/)|[@#])\S*)/g,'<a href="$2">$2</a>');
alert(a);
这导致:
blah blah blah blah<a href="http://example.com">http://example.com</a> blah blah<a href="#something">#something</a> blah blah<a href="https://example.com">https://example.com</a> blah blah<a href="@person">@person</a>
编辑:由于不应在href中复制用户名和哈希值,您可以使用三阶段查找和替换来执行此操作:
a = "blah blah blah blah http://example.com blah blah #something blah blah https://example.com blah blah @person";
a = a.replace(/(^|\s)(https?:\/\/\S*)/g,'<a href="$2">$2</a>');
a = a.replace(/(^|\s)[@](\S*)/g,'<a href="http://twitter.com/$2">$2</a>');
a = a.replace(/(^|\s)[#](\S*)/g,'<a href="https://twitter.com/search?q=%23$2">$2</a>');
alert(a);
这导致:
blah blah blah blah<a href="http://example.com">http://example.com</a> blah blah<a href="https://twitter.com/search?q=%23something">something</a> blah blah<a href="https://example.com">https://example.com</a> blah blah<a href="http://twitter.com/person">person</a>
答案 3 :(得分:0)
@tom你的解决方案一次只会解析一个字符串。但是twitter会在你打字时解析所有内容。
@Coop想要
使用此玩具here 后,这是我的方法 可以找到更多解释here
<div id="element">new text will drop here</div>
<script type="text/javascript">
$(document).ready(function(){
function convertTextToUrl(refinedText) {
var refinedText, refinePattern1, refinePattern2, refinePattern3, refinePattern4;
//URLs starting with http://, https://, or ftp://
refinePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
refinedText = refinedText.replace(refinePattern1, function(UrlTo) {
return UrlTo.link(UrlTo);
});
//URLs starting with www
refinePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
refinedText = refinedText.replace(refinePattern2, function(UrlTo) {
return UrlTo.link(UrlTo);
});
//mention with @
refinePattern3 = /[@]+[A-Za-z0-9-_]+/gim;
refinedText = refinedText.replace(refinePattern3, function(u) {
var username = u.replace("@","")
return u.link("http://twitter.com/"+username);
});
//# hastag
refinePattern4 = /[#]+[A-Za-z0-9-_]+/gim;
refinedText = refinedText.replace(refinePattern4, function(t) {
return t.link("https://twitter.com/"+t);
});
return refinedText;
} //USAGE
var TextToUrl= "Tubentertain is a web base #application, kind of a #device such as a #television or #multimedia player that has access to contents residing at remote sites.contact @tubentertain or visit http://tubentertain.com";
var newText=convertTextToUrl(TextToUrl);
$("#element").html(newText);
//========================
});
此功能对许多其他内容非常有用,例如将页面上的所有类似文本转换为链接到其他内容的网址。