Ticket #255 (new defect)

Opened 1 year ago

Last modified 6 months ago

RegExp: Syntax for referencing named backreference from within a replacement string?

Reported by: lth Assigned to: lth
Type: defect Priority: major
Milestone: Component: Proposals
Version: 4 Keywords:
Cc: brendan

Description

Cf msg by Steven Levithan on es4-discuss: both Python and Perl/.NET provide named submatches, and provide ways of referencing the names from replace strings. In ES3 we have $n and $nn within the replace string to extract and use submatches, it's likely an oversight that we don't have similar functionality for named submatches.

According to Steven, Python uses \g<name> and Perl/.NET use \k<name>, where I assume the angle brackets are literal. Neither makes much sense for ES4, since ES3 uniformly uses $ as the character signalling replacement text substitution. Considering the use of (?P<name>...) for matching, $<name> would not be bad.

Attachments

Change History

Changed 1 year ago by lth

See #266 for a related issue.

Changed 1 year ago by lth

I meant #256, of course.

Changed 1 year ago by StevenLevithan

Thanks for opening these tickets, lth. Note that Python's \g<name> and Perl/.NET's \k<name> are not equivalent. One is used for backreferences in a replacement string, while the other is used for a backreference in the regex. I've summarized the differences between named capture syntax in all current languages which support the feature under the heading "Named capture" at http://stevenlevithan.com/regex/xregexp/ (which is the documentation page for a forthcoming JavaScript? library I'm working on).

While I agree that $<name> would not be bad, personally I would recommend .NET's ${name}, since that already follows the prior art of some other regex libraries which use ${n} to remove ambiguity between backreferences, octals, and literal numbers.

Changed 6 months ago by airforce1

Note: See TracTickets for help on using tickets.