Detect URLs in text with JavaScript

后端 未结 13 2189
孤城傲影
孤城傲影 2020-11-22 06:23

Does anyone have suggestions for detecting URLs in a set of strings?

arrayOfStrings.forEach(function(string){
  // detect URLs in strings and do something sw         


        
13条回答
  •  温柔的废话
    2020-11-22 07:13

    Based on Crescent Fresh answer

    if you want to detect links with http:// OR without http:// and by www. you can use the following

    function urlify(text) {
        var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
        //var urlRegex = /(https?:\/\/[^\s]+)/g;
        return text.replace(urlRegex, function(url,b,c) {
            var url2 = (c == 'www.') ?  'http://' +url : url;
            return '' + url + '';
        }) 
    }
    

提交回复
热议问题