Closed Bug 1517997 Opened 5 years ago Closed 5 years ago

RegExp non-greedy quantifiers don't match the smallest possible match

Categories

(Developer Documentation Graveyard :: JavaScript, enhancement, P2)

All
Other
enhancement

Tracking

(Not tracked)

RESOLVED INCOMPLETE

People

(Reporter: luiscastro193, Unassigned, NeedInfo)

References

()

Details

:: Developer Documentation Request

      Request Type: Correction
     Gecko Version: unspecified
 Technical Contact: 

:: Details

At the quantifiers section, documentation says that non-greedy quantifiers match the smallest possible match. That is not true.

For example, /.*? test/g against "Whatever test" matches the full string.

They match the first valid substring they find from left to right.
greedy/non-greedy difference is only about the length after the first match,
not about the position of the first match.

first, "." matches to "W", then it looks for the first " test" match and stops, even if ".*" still matches to that part.
you can see the difference in the following 2 cases:

  "Whatever test test".match(/.* test/)  // = ["Whatever test test"]
  "Whatever test test".match(/.*? test/) // = ["Whatever test"]
Yeah I don't know the proper way of wording it but "smallest possible match" is not it.

In both cases the smallest possible match is clearly just " test". So reading "smallest possible match" in the most reliable JavaScript documentation source is very misleading (it was for me at least).
"shortest/smallest repeats after the first match" maybe?
I don't fully get it but it is ambiguous enough so that people understand it's complicated and they have to look it up.

In practice it just tells the engine to take the first valid match it finds. So if you know how the search works, you know what it means. But I don't know how to express it otherwise.
Priority: -- → P2

Closing; this is now being tracked at https://github.com/mdn/sprints/issues/999

Status: UNCONFIRMED → RESOLVED
Closed: 5 years ago
Resolution: --- → INCOMPLETE

I have had a go at clarifying the text here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#quantifiers.

Please let me know if you think it is better.

Flags: needinfo?(luiscastro193)
Flags: needinfo?(arai.unmht)

thanks, I think it's better :)

Flags: needinfo?(arai.unmht)
You need to log in before you can comment on or make changes to this bug.