Regular expression to find URLs within a string

前端 未结 27 2014
被撕碎了的回忆
被撕碎了的回忆 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:39

    Guess no regex is perfect for this use. I found a pretty solid one here

    /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm
    

    Some differences / advantages compared to the other ones posted here:

    • It does not match email addresses
    • It does match localhost:12345
    • It won't detect something like moo.com without http or www

    See here for examples

提交回复
热议问题