Detect URLs in text with JavaScript

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

    You can use a regex like this to extract normal url patterns.

    (https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})
    

    If you need more sophisticated patterns, use a library like this.

    https://www.npmjs.com/package/pattern-dreamer

提交回复
热议问题