Regular expression to find URLs within a string

前端 未结 27 1803
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 14:18

Does anyone know of a regular expression I could use to find URLs within a string? I\'ve found a lot of regular expressions on Google for determining if an entire string is

27条回答
  •  情话喂你
    2020-11-22 14:49

    This is a slight improvement on/adjustment to (depending on what you need) Rajeev's answer:

    ([\w\-_]+(?:(?:\.|\s*\[dot\]\s*[A-Z\-_]+)+))([A-Z\-\.,@?^=%&:/~\+#]*[A-Z\-\@?^=%&/~\+#]){2,6}?
    

    See here for an example of what it does and does not match.

    I got rid of the check for "http" etc as I wanted to catch url's without this. I added slightly to the regex to catch some obfuscated urls (i.e. where user's use [dot] instead of a "."). Finally I replaced "\w" with "A-Z" to and "{2,3}" to reduce false positives like v2.0 and "moo.0dd".

    Any improvements on this welcome.

提交回复
热议问题