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

Support multiple match groups #11

Merged
merged 1 commit into from Nov 1, 2015

Conversation

Projects
None yet
2 participants
@Krusen
Contributor

Krusen commented Sep 7, 2015

Added support for multiple capture groups in regex expressions.

Example: /(group1)|(group2)|(group3)/gim executed on the string "group3" will return ["group3", undefined, undefined, "group3"] which will now work correctly.

@AndiDittrich

This comment has been minimized.

Member

AndiDittrich commented Sep 11, 2015

Dear Soren,

thanks for your contribution. i have to check if it's working without side-effects to the tokenzier.

@AndiDittrich AndiDittrich added this to the v3.0.0 milestone Sep 16, 2015

@AndiDittrich AndiDittrich self-assigned this Sep 16, 2015

AndiDittrich added a commit that referenced this pull request Nov 1, 2015

@AndiDittrich AndiDittrich merged commit 8244390 into EnlighterJS:master Nov 1, 2015

@AndiDittrich AndiDittrich modified the milestones: v2.10.0, v3.0.0 Nov 1, 2015

@AndiDittrich

This comment has been minimized.

Member

AndiDittrich commented Nov 1, 2015

i've modified your code to support multiple matching groups - now each group is rendered as single token. but you have to take care of the regex to avoid overlapping matches.

// matching groups used ?
if (match.length == 1) {
    rawTokens.push(token(match[0], rule.alias, match.index));
// use full pattern
}else{
    // get first matched group
    for (var i = 1; i < match.length; i++) {
        if (match[i] && match[i].length > 0){
            rawTokens.push(token(match[i], rule.alias, match.index + match[0].indexOf(match[i])));
        }
    }
}

For example, the following pattern:

'test': {
    pattern: /(.)?(BYTE|CSEG|DEF)/gim,
    alias: 'kw4'
},

will render

.DEF temp=R16
.DEF ior=R0
.CSEG

to

.(kw4)DEF(kw4) temp=R16(text)
.(kw4)DEF(kw4) ior=R0(text)
.(kw4)CSEG(kw4)

i think this modification allows a more universal usage

AndiDittrich added a commit that referenced this pull request Nov 1, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment