A regex that validates a web address and matches an empty string?

前端 未结 3 1784
一个人的身影
一个人的身影 2020-12-18 13:06

The current expression validates a web address (HTTP), how do I change it so that an empty string also matches?

(http|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+         


        
3条回答
  •  误落风尘
    2020-12-18 13:39

    Put the whole expression in parenthesis and mark it as optional (“?” quantifier, no or one repetition):

    ((http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)?
    

提交回复
热议问题