Detect URLs in text with JavaScript

后端 未结 13 2015
孤城傲影
孤城傲影 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 06:59

    tmp.innerText is undefined. You should use tmp.innerHTML

    function strip(html) 
        {  
            var tmp = document.createElement("DIV"); 
            tmp.innerHTML = html; 
            var urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;   
            return tmp.innerHTML .replace(urlRegex, function(url) {     
            return '\n' + url 
        })
    

提交回复
热议问题