1

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?

serpent403
  • 803
  • 16
  • 32

2 Answers2

1

Maybe this way:

/([^:]*):\/\/([^\/]*)(.*)/

Now $2 should be just a domain.

Krzysztof Jabłoński
  • 1,890
  • 1
  • 20
  • 29
  • Sorry, I forgot javascript has got no possessive quantifiers (++, ?+, *+), like java do. Possessive quantifier would be even more suitable, hence there is no other option, thus no backtracking is necessary at all. Without it regex is just as optimal for a case there is a match, but much slower in case it should fail. Look [here](http://stackoverflow.com/questions/4077135/writing-better-regex-expression-for-not-using-lazy-repeat-quantifier) for similar issue. – Krzysztof Jabłoński Sep 19 '12 at 13:50
0

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).

helpermethod
  • 59,493
  • 71
  • 188
  • 276