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.
/(=1)?$/would work.?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./(=1)/wouldn't work?