Detect URLs in text with JavaScript

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

    First you need a good regex that matches urls. This is hard to do. See here, here and here:

    ...almost anything is a valid URL. There are some punctuation rules for splitting it up. Absent any punctuation, you still have a valid URL.

    Check the RFC carefully and see if you can construct an "invalid" URL. The rules are very flexible.

    For example ::::: is a valid URL. The path is ":::::". A pretty stupid filename, but a valid filename.

    Also, ///// is a valid URL. The netloc ("hostname") is "". The path is "///". Again, stupid. Also valid. This URL normalizes to "///" which is the equivalent.

    Something like "bad://///worse/////" is perfectly valid. Dumb but valid.

    Anyway, this answer is not meant to give you the best regex but rather a proof of how to do the string wrapping inside the text, with JavaScript.

    OK so lets just use this one: /(https?:\/\/[^\s]+)/g

    Again, this is a bad regex. It will have many false positives. However it's good enough for this example.

    function urlify(text) {
      var urlRegex = /(https?:\/\/[^\s]+)/g;
      return text.replace(urlRegex, function(url) {
        return '' + url + '';
      })
      // or alternatively
      // return text.replace(urlRegex, '$1')
    }
    
    var text = 'Find me at http://www.example.com and also at http://stackoverflow.com';
    var html = urlify(text);
    
    console.log(html)

    // html now looks like:
    // "Find me at http://www.example.com and also at http://stackoverflow.com"
    

    So in sum try:

    $$('#pad dl dd').each(function(element) {
        element.innerHTML = urlify(element.innerHTML);
    });
    

提交回复
热议问题