I am trying to fetch 'domain' from 'http://domain.com/some-more-path' using regex:
/(.*)(http|https):\/\/(.*)(\/)(.*)/ and then $3
My issue is instead of 'domain' I get 'domain/some-more-path'. What am I doing wrong?
I am trying to fetch 'domain' from 'http://domain.com/some-more-path' using regex:
/(.*)(http|https):\/\/(.*)(\/)(.*)/ and then $3
My issue is instead of 'domain' I get 'domain/some-more-path'. What am I doing wrong?
Maybe this way:
/([^:]*):\/\/([^\/]*)(.*)/
Now $2 should be just a domain.
Change
/(.*)(http|https):\/\/(.*)(\/)(.*)/
to
/(.*)(http|https):\/\/(.*?)(\/)(.*)/
The problem is the .*
part. *
is a greedy quantifier and will consume as much characters as possible. If you put a ?
behind the *
you switch the behaviour of the qualifier to non-greedy (i.e. it consumes only as much characters as needed).