Regex for url validation with parts capturing

前端 未结 2 568
谎友^
谎友^ 2021-01-19 11:11

Can a single regex be used to valdate urls and match all the parts, I have been working on one and what I have come up with so far is:

(?:(?P[a         


        
2条回答
  •  盖世英雄少女心
    2021-01-19 11:40

    Can a single regex be used to valdate urls and match all the parts

    No.

    strager's regex is impressive, but at the end of the day it's less readable, maintainable and reliable than just using a proper URI parser. It necessarily rejects valid URIs and accepts strings that are not URIs, because the rules of formatting URIs cannot be fully expressed in a regex.

    mailto://user@unkwndesign.com

    There shouldn't be a '//' in a mailto URI. You can't tell what format the remainder (post-:) of the URI is going to be until you've read the scheme; many URI schemes do not conform to the credentials@host/path format. Best to accept only specific schemes where you know how to parse their URIs.

提交回复
热议问题