我正在使用:
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp,"<a href='$1' target='internet'>$1</a>");
将例如 http://www.example.com
变成可点击的链接。这很好用!
有谁知道如何扩展此表达式以同时捕获 www.example.com
(即没有 http://
)?
最佳答案
这对我有用:
在这里添加一个( )*
((https?|ftp|file):\/\/)*
http://jsfiddle.net/jasongennaro/NBWyr/
基本上 ()
中的零个或多个项目
关于javascript - 如何从字符串创建 URL 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7046675/