【发布时间】:2011-10-09 18:13:49
【问题描述】:
假设我有这个文本:
Hi, my name is Pablo and my home page is @987654321@
如何检测和验证 URL,以便它自动将其包装到 <a href="the detected url">the detected url</a> 中?
谢谢!
【问题讨论】:
标签: javascript jquery html
假设我有这个文本:
Hi, my name is Pablo and my home page is @987654321@
如何检测和验证 URL,以便它自动将其包装到 <a href="the detected url">the detected url</a> 中?
谢谢!
【问题讨论】:
标签: javascript jquery html
你可以使用正则表达式:
var s = 'Hi, my name is Pablo and my home page is http://zad0xsis.net';
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
s = s.replace(exp,"<a href=\"$1\">$1</a>");
alert(s);
【讨论】:
也可以试试jquery linkify插件@http://code.google.com/p/jquery-linkify/
易于使用 -
$('.section').linkify();
与达林所说的差不多。
【讨论】: