简体   繁体   中英

Javascript Regex Match the first the occurrence

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

Make the first * ungreedy

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

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM