简体   繁体   English

JavaScript正则表达式仅捕获最后一次出现的组

[英]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 : 我创建了一个JavaScript正则表达式:

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

( Regex101 demo page here ) Regex101演示页面在这里

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'). 我成功地检索了第一个选项类型(在上面的示例中为“ String”和“ Boolean”),但是就正则表达式的第二部分而言,我只能检索最后一个组(“ 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 ? 有没有办法在JS正则表达式中做到这一点?

"Is there a way to do that [access previous captures of a certain group] in a JS regex?" “有没有办法在JS正则表达式中[访问特定组的先前捕获]?” - 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 ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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