Javascript JS在一个字符串中获取多个链接
我正在尝试使用以下格式的多个链接转换字符串:Javascript JS在一个字符串中获取多个链接,javascript,regex,url,hyperlink,find,Javascript,Regex,Url,Hyperlink,Find,我正在尝试使用以下格式的多个链接转换字符串: random text and a link: [Google](http://google.com), also check out this link: [Facebook](http://facebook.com) which can all be found here: http://example.com 为此: random text and a link: <a href="http://google.com">Googl
random text and a link: [Google](http://google.com),
also check out this link: [Facebook](http://facebook.com)
which can all be found here: http://example.com
为此:
random text and a link: <a href="http://google.com">Google</a>,
also check out this link: <a href="http://facebook.com">Facebook</a>
which can all be found here: <a href="http://example.com">http://example.com</a>
随机文本和链接:,
也请查看此链接:
这些都可以在这里找到:
现在我有一个函数,只能找到第一个链接
function findURL(comment) {
//Find text in round brackets
var roundBrackets = /\(([^)]+)\)/
var matches = roundBrackets.exec(comment)
if(matches != null && matches[1] != undefined && ~matches[1].indexOf("http")){
//If found link in round brackets, see if it's accompanied by text in square brackets
var squareBrackets = /\[([^)]+)\]/
var text = squareBrackets.exec(comment)
var linkText = matches[1] //Will be overwritten if there is text in square brackets
if(text != null){
linkText = text[1]
comment = comment.replace(text[0], '')
}
var link = '<a href="'+matches[1]+'">'+linkText+'</a>'
comment = comment.replace(matches[0], link)
}
//Find regular links
else if(comment && ~comment.indexOf("http")){
var urlRegex = /(https?:\/\/[^\s]+)/g
var url = urlRegex.exec(comment)
var newLink = '<a href="'+url[1]+'">'+url[1]+'</a>'
comment = comment.replace(url[1], newLink)
}
return comment
}
函数findURL(注释){
//查找圆括号中的文本
变量圆括号=/\([^)]+)\)/
var matches=round括号.exec(注释)
if(matches!=null&&matches[1]!=undefined&&matches[1].indexOf(“http”)){
//如果在圆括号中找到链接,请查看是否有方括号中的文本
变量方括号=/\[([^)]+)\]/
var text=方括号.exec(注释)
var linkText=matches[1]//如果方括号中有文本,将被覆盖
如果(文本!=null){
linkText=文本[1]
comment=comment.replace(文本[0],“”)
}
变量链接=“”
comment=comment.replace(匹配[0],链接)
}
//查找常规链接
else if(comment&&~comment.indexOf(“http”)){
var urlRegex=/(https?:\/\/[^\s]+)/g
var url=urlRegex.exec(注释)
var newLink=''
comment=comment.replace(url[1],newLink)
}
回复评论
}
我觉得这可能不是找到链接的最佳方法,如果有更有效的方法,我不介意将整个内容全部更改。另一个想法,但不会比你的想法更强大、更低效
var r = "random text and a link: [Google](http://google.com),
also check out this link: [Facebook](http://facebook.com)
which can all be found here: http://example.com";
var o = r.match(new RegExp(/(\[[\w\-\.]+\]+)(\(?http:[\/\w\-\.]+\))|(http:[\/\w\-\.]+)/g));
var v = r;
for(i in o)
{
if(o[i].indexOf("]") >= 0)
{
var p = o[i].replace(/[\[\]\(\)]/g, ' ').trim().split(' ');
v = v.replace(o[i], "<a href='"+p.pop()+"'>"+p.shift()+"</a>");
}else
{
v = v.replace(o[i], "<a href='"+o[i]+"'>"+o[i]+"</a>");
}
}
var r=“随机文本和链接:[谷歌](http://google.com),
也请查看此链接:[Facebook](http://facebook.com)
这些都可以在这里找到:http://example.com";
var o=r.match(新RegExp(/(\[\w\-\.]+\]+)(\(?http:[\/\w\-\.]+\))(http:[\/\w\-\.]+)/g);
var v=r;
for(i in o)
{
如果(o[i].indexOf(“]”)大于等于0)
{
var p=o[i].replace(/[\[\]\(\)]/g'').trim().split('');
v=v.替换(o[i],“”);
}否则
{
v=v.替换(o[i],“”);
}
}
输出
"random text and a link: <a href='http://google.com'>Google</a>,
also check out this link: <a href='http://facebook.com'>Facebook</a>
which can all be found here: <a href='http://example.com'>http://example.com</a>"
“随机文本和链接:,
也请查看此链接:
这些都可以在这里找到:“