tc39 / proposal-regexp-named-groups

Named capture groups for JavaScript RegExps
https://tc39.github.io/proposal-regexp-named-groups/
222 stars 21 forks source link

Reject bad replacement strings #32

Closed gibson042 closed 7 years ago

gibson042 commented 7 years ago

https://tc39.github.io/proposal-regexp-named-groups/#table-45 specifies very careful treatment of replacement strings including $<, but I'd rather see code like '2015-01-02'.replace(re, '$<month> $<day, $<year>') and '2015-01-02'.replace(re, '$<month> $<day>, $<year') call attention to developer errors by throwing exceptions rather than masking them with identity- or empty-string replacements.

IOW,

  1. If namedCaptures is undefined, the replacement text is the String "$<".
  2. Otherwise,
    1. Scan until the next >.
    2. If none is found, the replacement text is the String "$<"throw a SyntaxError exception.
    3. Otherwise,
      1. Let the enclosed substring be groupName.
      2. If groupName does not have the form of a RegExpIdentifierName, throw a SyntaxError exception.
      3. Let capture be ? Get(namedCaptures, groupName).
      4. If capture is undefined, replace the text through > with the empty string.
      5. Otherwise, replace the text through this following > with ? ToString(capture).
littledan commented 7 years ago

We actually just recently adopted the semantics you're raising this issue about, in https://github.com/tc39/proposal-regexp-named-groups/issues/29 . We've already been back and forth once, and implementations are already starting to ship, so I'd really prefer not to revisit this again. cc @msaboff @schuay

gibson042 commented 7 years ago

Hmm, just missed it I suppose. Damn.