Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $<n> and $<name> as alternative backreference syntax in replacement text #181

Merged
merged 9 commits into from Apr 25, 2017

Conversation

josephfrazier
Copy link
Collaborator

Fixes #177

I had some trouble using a single regex for the replacement, so I replaced
the $ syntax first in a separate pass.

src/xregexp.js Outdated
var replacementTokens = [
/\$(?:<([\w$]+)>)/g,
/\$(?:{([\w$]+)}|(\d\d?|[\s\S]))/g
];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather do this in one pass (rather than 2 or 3) for speed. But that aside, as written it doesn't make sense to do this in two passes since you're doing the same handling for $2 in the replacer function even when it isn't relevant.

I think what you want here is to make replacementToken /\$(?:{([\w$]+)}|<([\w$]+)>|(\d\d?|[\s\S]))/g, then change replacer ($0, $1, $2) to replacer($0, $1, $2, $3) or something like replacer($0, bracketed, angled, dollarToken), then put bracketed = bracketed || angled at the top of the replacer function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's exactly right! Thanks for the suggestion, I've pushed some updates implementing it.

josephfrazier added a commit to josephfrazier/xregexp that referenced this pull request Apr 25, 2017
josephfrazier added a commit to josephfrazier/xregexp that referenced this pull request Apr 25, 2017
src/xregexp.js Outdated
function replacer ($0, $1, $2, $3) {
$1 = $1 || $2
function replacer ($0, bracketed, angled, dollarToken) {
bracketed = bracketed || angled
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a trailing semicolon

src/xregexp.js Outdated
@@ -1157,6 +1157,8 @@ XRegExp.matchChain = function(str, chain) {
* backreference n/nn.
* - ${n} - Where n is a name or any number of digits that reference an existent capturing
* group, inserts backreference n.
* - $<n> - Where n is a name or any number of digits that reference an existent capturing
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above where $&, $0 are on the same line. Would you mind doing the same here, and grouping this with ${n}?


// Backreference to a nonparticipating capturing group
expect('test'.replace(XRegExp('t|(?<test>t)', 'g'), ':${test}:')).toBe('::es::');
expect('test'.replace(XRegExp('t|(?<test>t)', 'g'), ':$<test>:')).toBe('::es::');
});

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for mixing ${n} and $<n> syntax in the same replacement string?

@slevithan
Copy link
Owner

slevithan commented Apr 25, 2017

This is looking good.

In addition to addressing the few minor comments I just added, can you rebase this and fix the conflict?

This branch cannot be rebased due to conflicts
Rebasing the commits of this branch on top of the base branch cannot be performed automatically due to conflicts encountered while reapplying the individual commits from the head branch.

@slevithan slevithan merged commit 5187f31 into slevithan:master Apr 25, 2017
slevithan pushed a commit that referenced this pull request Apr 25, 2017
slevithan pushed a commit that referenced this pull request Apr 25, 2017
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
@josephfrazier josephfrazier deleted the backreference-angle-bracket branch February 18, 2018 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants