Regular expression for recognizing url

前端 未结 3 750
你的背包
你的背包 2020-12-20 06:56

I want to create a Regex for url in order to get all links from input string. The Regex should recognize the following formats of the url address:

  • http(s)://w
3条回答
  •  半阙折子戏
    2020-12-20 07:18

    I don't know why your result in match is only http:// but I cleaned your regex a bit

    ((?:(?:https?|ftp|gopher|telnet|file|notes|ms-help):(?://|\\\\)(?:www\.)?|www\.)[\w\d:#@%/;$()~_?\+,\-=\\.&]+)
    

    (?:) are non capturing groups, that means there is only one capturing group left and this contains the complete matched string.

    (?:(?:https?|ftp|gopher|telnet|file|notes|ms-help):(?://|\\\\)(?:www\.)?|www\.) The link has now to start with something fom the first list followed by an optional www. or with an www.

    [\w\d:#@%/;$()~_?\+,\-=\\.&] I added a comma to the list (otherwise your long example does not match) escaped the - (you were creating a character range) and unescaped the . (not needed in a character class.

    See this here on Regexr, a useful tool to test regexes.

    But URL matching is not a simple task, please see this question here

提交回复
热议问题