Regex to match URL

后端 未结 14 2469
耶瑟儿~
耶瑟儿~ 2020-11-22 09:41

I am using the following regex to match a URL:

$search  = \"/([\\S]+\\.(MUSEUM|TRAVEL|AERO|ARPA|ASIA|COOP|INFO|NAME|BIZ|CAT|COM|INT|JOBS|NET|ORG|PRO|TEL|AC|A         


        
14条回答
  •  忘掉有多难
    2020-11-22 09:50

    This question was surprisingly difficult to find an answer for. The regexes I found were too complicated to understand, and anything more that a regex is overkill and too difficult to implement.

    Finally came up with:

    /(\S+\.(com|net|org|edu|gov)(\/\S+)?)/
    

    Works with http://example.com, https://example.com, example.com, http://example.com/foo.

    Explanation:

    • Looks for .com, etc.
    • Matches everything before it up to the space
    • Matches everything after it up to the space

提交回复
热议问题