jQuery detect and validate an URL

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

Let's imagine I have this text:

Hi, my name is Pablo and my home page is http://zad0xsis.net

How can I detect and validate the URL so it automatically wraps it into <a href="the detected url">the detected url</a>? Thanks!

回答1:

You could use a regex:

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); 

Live demo.



回答2:

Can also try the jquery linkify plugin @ http://code.google.com/p/jquery-linkify/

Easy to use -

$('.section').linkify(); 

Pretty much does the same as mentioned by Darin.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!