views:

96

answers:

3

Hi,

I've this regex (which doesn't do what i want): /^.*\/(eu|es)(?:\/)?([^#]*).*/ which actually is the js version of: /^.*/(eu|es)(?:/)?([^#]*).*/

Well, it doesn't do what i want, of course it works. :) Given this URLs:

  • http://localhost/es -> [1] = es, [2] = ''
  • http://localhost/eu/bla/bla#wop -> [1] = eu, [2] = 'bla/bla'
  • http://localhost/eu/bla/eubla -> [1] = eu, [2] = 'bla'

The first two urls work as i expected. The third one is not doing what i want. As "eu" is found later on the url, it does the match with the second eu instead of the first one. So I would like it to match this: [1] = 'eu', [2] = 'bla/eubla'

How must I do it?

Thank you. :)

+1  A: 

I think what you want is nongreedy repetition for the first repetition character *. Try this:

/^.\*?\/(eu|es)(?:\/)?([^#]\*).\*/

The only difference is the question mark ? after the first *. If this is missing, the * will match as many characters as possible, leading to the undesired behaviour in your third example.

Tom Bartel
+2  A: 

Make the first * ungreedy

/^.\*?\/(eu|es)(?:\/)?([^#]\*).\*/

Btw, do you really need to escape * in javascript? Won't this work?

/^.*?\/(eu|es)(?:\/)?([^#]*).*/
Amarghosh
Why is the star escaped?
Tim Pietzcker
Yeah, I was wondering the same thing.
Tom Bartel
Fixed. They were no intended to be there, it's just been a confusion that I had with editor preview.
doup
Anyway, thanks for the solution. :)
doup
+1  A: 

The above answers re the greedy versus non-greedy are fine, but why bother matching the start of the URL anyway? Just start your expression with:

/\/(eu|es)

and it will match the first one found on the line.

JimG
True, this also works. Anyway, it's nice to know the ungreedy thing. Thanks to all. :)
doup