开发者

javascript regex to parse urls without protocol

开发者 https://www.devze.com 2023-04-08 00:08 出处:网络
String.prototype.linkify = function() { this.replace(/((h开发者_StackOverflow社区t|f)tp:\\/\\/)?([^:\\/\\s]+)\\w+.(com|net|org)/gi, \'<a href=\"$&\">$&</a>\')
String.prototype.linkify = function() {
this.replace(/((h开发者_StackOverflow社区t|f)tp:\/\/)?([^:\/\s]+)\w+.(com|net|org)/gi, '<a href="$&">$&</a>')
}

with http://www.google.com http://yahoo.com www.facebook.com it matches them all, however I want facebook to be prepended with the protocol group if it does not exist. Is there a way to do this without doing two .replace ?


I would do something like this:

String.prototype.linkify = function () {
  return this.replace(/((?:ht|f)tp:\/\/)?([^:\/\s]+\w+\.(?:com|net|org))/gi, function (_, protocol, rest) {
    var url = (protocol || "http://") + rest
    return '<a href="' + url + '">' + url + '</a>'
  })
}

(I fixed a couple of other problems with your code: you were missing a return and you were matching the domain name period using . rather than \..)

And I assume I don’t need to point out how poorly this will match URL:s in general, due to a number of problems with your pattern.


If you don't actually need to match FTP URLs, you can just assume the "http://" section of the link. This regex does that, while allowing you to also use https.

this.replace(/(http(s)?:\/\/)?(([^:\/\s]+)\.(com|net|org))/gi,
                         '<a href="http$2://$3">http$2://$3</a>')

I'm not sure what your use case is, but I'd like to note this regex will fail on the following urls:

  • http://google.com/spam = <a href="http://google.com">http://google.com</a>/spam
  • http://google.ca = no match

This is because you're using few hardcoded tlds (com, net, org), and aren't matching any characters after the domain.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号