jQuery detect and validate an URL

前端 未结 2 1913
不知归路
不知归路 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,"$1"); 
    alert(s);
    

    Live demo.

提交回复
热议问题