3

I am having trouble making zero or one '?' give preference to one occurrence over zero, in javascript.

Ex. This is my regex: (=1)?

This is my string, str: abcd=1

when i do regex.exec(str) I get the following returned: ["",undefined]. It looks like it's choosing the zero length match in the beginning of my string. Is there a way to get it to choose '=1'? Possibly this may work differently in other languages but I'm currently using javascript, and this seems to be the case.

3
  • 1
    Have you tried anchoring the expression? E.g. /(=1)?$/ would work. Commented Jun 11, 2013 at 16:41
  • 1
    The reason you get an empty match is not that the ? is not that it's ungreedy but that before any kind of greediness or ungreediness plays a role, matches are tried from left to right. And since your pattern allows for zero-width matches anywhere, you will always get the empty match at the beginning of the string first. As nderscore said in his answer, just leave out the quantifier altogether. Commented Jun 11, 2013 at 16:43
  • 3
    Could you share a few use cases where /(=1)/ wouldn't work? Commented Jun 11, 2013 at 16:44

3 Answers 3

2

The expression (=1)? will give precedence to one occurrence over zero, but the regular expression engine will always attempt to match as early in the string as possible. So starting at the first character in the string, first it will try to match =1 and fail, and then because of the ? it will match the empty string.

I think the following expression is most similar to what your intention is:

(?:.*(=1))?

This will put =1 into the first capture group if it is anywhere in the string, but every string will still be matched because of the ? making the non-capturing group optional.

Sign up to request clarification or add additional context in comments.

Comments

2

By defaut ? is greedy in javascript. Your problem is somewhere else.

aside note: to have ? lazy you must write ?? (like other quantifiers)

Comments

0

(=1)* will match =1=1=1=1=1=1=1=1

If you're looking to match all digits after the = then use

abcde=(\d+)

This will place all the digits into capture group 1.

4 Comments

I'm not sure if this has changed since then, but now it's not true imgur.com/wp8Xc4C
In your examples (=1)* successfully matched at the first character because the * part of your expression is looking for zero or more of the characters inside the parentheses. Since the string, you were matching against started with abcd regex was happy with zero matches at the first character and stopped evaluating the rest of the string hence the result was an empty string.
The other example (=1)? has the same condition but here the ? character means zero or one of the characters inside the preceding parentheses.
yeah, but doesn't that mean that (=1)* will match =1=1=1=1=1=1=1=1 is not true? it matched empty string, not =1=1=1=1=1=1=1=1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.