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:
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. :)
You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" . This means that if for instance nothing comes after the ".
There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else.
A Regex or regexp (short for regular expression) is a sequence of characters that define a search pattern – it is mostly used for pattern matching with strings. Regex is supported by most programming languages. Dart provides this support through its RegExp class.
In the context of regular expressions, a character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.
Make the first *
ungreedy
/^.\*?\/(eu|es)(?:\/)?([^#]\*).\*/
Btw, do you really need to escape *
in javascript? Won't this work?
/^.*?\/(eu|es)(?:\/)?([^#]*).*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With