简体   繁体   中英

JavaScript regex captures only the last group occurrence

I am trying to parse a documentation text, which is formatted like this :

className : String
A text
colHeaders : Boolean (default false) 
colHeaders : Array [A, B, C, ...] 
colHeaders : Function function(index) { return ... }
Another text

( Full documentation here )

So every option in the documentation can be of multiple types, and I want to programatically recover them. I created a JavaScript regex :

^(\w+) : (\w+)[^\n]*(?:\n\1 : (\w+)[^\n]*)*

( Regex101 demo page here )

I successfully retrieve the first option type ('String' and 'Boolean' in the example above), but as far as the second part of the regex is concerned I can only retrieve the last group ('Function', where I would like both 'Array' and 'Function').

If I remove the '*' quantifier at the end of the regex, I only retrieve 'Array' (same thing if I add the non-greedy symbol '?' after it), but once again I would like both. Is there a way to do that in a JS regex ?

"Is there a way to do that [access previous captures of a certain group] in a JS regex?" - No.

You can however use two regexes - one that captures blocks:

/^(\w+) : .*(?:\n^\1.*)*/gm

and one that parses a block into lines:

/^\w+ : (\w+)\s*(.*)/gm

as in

var str = [
        'className : String',
        'A text',
        'colHeaders : Boolean (default false)',
        'colHeaders : Array [A, B, C, ...]',
        'colHeaders : Function function(index) { return ... }',
        'Another text'
    ].join("\n"),
    reBlock = /^(\w+) : .*(?:\n^\1.*)*/gm,
    reLine = /^\w+ : (\w+)\s*(.*)/gm,
    block, line;

while (block = reBlock.exec(str)) {
    console.log(block[1]);
    while (line = reLine.exec(block[0])) {
        console.log(" - ", line[1], line[2]);
    }
}

prints

className
  -  String 
 colHeaders
  -  Boolean (default false)
  -  Array [A, B, C, ...]
  -  Function function(index) { return ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM