jQuery detect and validate an URL

前端 未结 2 1896
不知归路
不知归路 2021-01-20 19:20

Let\'s imagine I have this text:

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

相关标签:
2条回答
  • 2021-01-20 19:41

    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.

    0 讨论(0)
  • 2021-01-20 19:43

    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.

    0 讨论(0)
提交回复
热议问题