Javascript Regex Match the first the occurrence

javascript, regex

Solution

Make the first `*` ungreedy

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

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

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

Problem

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

Original source