提取并添加指向字符串中 URL 的链接
我有几个带有链接的字符串。例如:
var str = "I really love this site: http://www.stackoverflow.com"
我需要添加一个链接标签,这样 str 将是:
I really love this site: <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>
I have several strings that have links in them. For instance:
var str = "I really love this site: http://www.stackoverflow.com"
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种可能是使用 URI 类让它进行解析。类似这样的事情:
One possibility would be to use the URI class to let it do the parsing. Something along the lines of this:
您可以使用 URI.extract 来实现这一点:
上面的代码也匹配一个字符串中的多个 URL。
You can use URI.extract for this:
The above also matches multiple URLs in one string.
在这里,工作代码:) 去掉显示链接中的 http/s 前缀,还
请注意,您应该在 uri+" " 上进行正则表达式,以便它正确捕获链接...然后您需要在开头添加一个空格来捕获末尾没有尾随空格的链接...
Here you go, working code :) strips out the http/s prefix in the displayed link also
note you should regex on uri+" " so it catches the links properly... and then you need to add a space at the beginning to catch links at the end that don't have a trailing space...