Regular expression for URL

后端 未结 10 2073
刺人心
刺人心 2020-12-03 01:04

I have written regex to validate URL which could be either like

  • example.com

  • www.example.com

  • http://www.example.com

10条回答
  •  猫巷女王i
    2020-12-03 01:29

    when creating a regex for strict matching, you must make it sure it begins with '^' and finishes with '$'. Otherwise, the regex will see if it finds a substring which matches.

    • '^' means match begin of line
    • '$' means match end of line.

    However, using a regex to match an URL is error prone : better rely on existing frameworks doing it better (there are a lot of potential traps in url, like parameters, unkown domains, url containing ip instead of domain....)

提交回复
热议问题