Skip to content

Support multiple match groups#11

Merged
AndiDittrich merged 1 commit intoEnlighterJS:masterfrom
Krusen:multiple-match-groups
Nov 1, 2015
Merged

Support multiple match groups#11
AndiDittrich merged 1 commit intoEnlighterJS:masterfrom
Krusen:multiple-match-groups

Conversation

@Krusen
Copy link
Copy Markdown
Contributor

@Krusen 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
Copy link
Copy Markdown
Member

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 pushed 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
Copy link
Copy Markdown
Member

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants