提取并添加指向字符串中URL的链接 [英] Extract and add link to URLs in string

查看:62
本文介绍了提取并添加指向字符串中URL的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何用链接替换普通URL?





<我有几个字符串,里面有链接。例如:

I have several strings that have links in them. For instance:

var str = "I really love this site: http://www.stackoverflow.com"

我需要添加一个链接标签,因此str将是:

and I need to add a link tag to that so the str will be:

I really love this site: <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>

我想会有一些正则表达式,但我无法让它为我工作比赛()。任何其他想法

I imagine there would be some regex involved, but I can't get it to work for me with match(). Any other ideas

推荐答案

这很简单:

str.replace( /(http:\/\/[^\s]+)/gi , '<a href="$1">$1</a>' )

输出:

I really love this site: <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>

这篇关于提取并添加指向字符串中URL的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆