Detect URLs in text with JavaScript

后端 未结 13 2005
孤城傲影
孤城傲影 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:22

    There is existing npm package: url-regex, just install it with yarn add url-regex or npm install url-regex and use as following:

    const urlRegex = require('url-regex');
    
    const replaced = 'Find me at http://www.example.com and also at http://stackoverflow.com or at google.com'
      .replace(urlRegex({strict: false}), function(url) {
         return '<a href="' + url + '">' + url + '</a>';
      });
    
    0 讨论(0)
提交回复
热议问题