Regular expression that validates a web address and matches an empty string?

The current expression checks the web address (HTTP), how can I change it so that the empty string also matches?

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

If you want to change the expression to completely or completely remove the empty line or , you will need to use the anchor metacharacters ^ and $ (which correspond to the beginning and end of the line, respectively).

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

As shown in the figure, you can simplify your protocol a bit for the protocol, so I have included it for you as well.

Although, if you use this expression from a program or script, it may be easier for you to use languages โ€‹โ€‹for your own means of checking for empty input.

// in no particular language...
if input.length > 0 then
    if input matches <regex> then
        input is a URL
    else
        input is invalid
else
    input is empty
+5

( "?" , ):

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

Expr? Expr - URL-. http https: https?. ? - . Wikipedia:

? , .

0

Source: https://habr.com/ru/post/1703914/


All Articles