javascript - 尝试在 JavaScript 正则表达式中重复捕获 block

标签 javascript regex

下面是我的 body 。基本上每个 block 都由一条线分隔,上面没有任何内容。每个 block 都有一个位于括号中的标题,然后可以具有任意数量的属性,其格式为左侧单词,然后等于,然后右侧单词。

[General]
StartWithLastProfile=0

[Profile0]
Name=default
IsRelative=1
Path=Profiles/vr10qb8s.default
Default=1

[Profile1]
Name=cleanER One Here
IsRelative=1
Path=Profiles/k46wtieb.cleanER One Here

我正在尝试获得 3 场比赛。每个应该看起来像:[整个匹配,标题,prop1,val1,propN,valN]

比赛1:

['[General]
StartWithLastProfile=0','General','StartWithLastProfile','0']

比赛2:

['[Profile0]
Name=default
IsRelative=1
Path=Profiles/vr10qb8s.default
Default=1','Profile0','Name','default','IsRelative','1','Path','Profiles/vr10qb8s.default','Default','1']

等等。

这是我的正则表达式:

       var patt = /\[.*\](?:\s+?([\S]+)=([\S]+)+/mg;
       var blocks = [];

       var match;
       while (match = patt.exec(readStr)) {
        console.log(match)
       }

但是这是吐出来​​的:[整个匹配,标题,propLAST,valLAST];。如果我将正则表达式 patt 中的最后一个 + 更改为 +?然后它给出[整个匹配,标题,propFIRST,valFIRST];

这个正则表达式可以工作,但有一个花招:

var patt = /\[.*\](?:\s+?([\S]+)=([\S]+))(?:\s+?([\S]+)=([\S]+))?(?:\s+?([\S]+)=([\S]+))?(?:\s+?([\S]+)=([\S]+))?(?:\s+?([\S]+)=([\S]+))?/mg;

现在返回:

[ "[General]
StartWithLastProfile=0", "StartWithLastProfile", "0", undefined, undefined, undefined, undefined, undefined, undefined, undefined, 1 more… ]

[ "[Profile0]
Name=default
IsRelative=1
Path=Profiles/vr10qb8s.default
Default=1", "Name", "default", "IsRelative", "1", "Path", "Profiles/vr10qb8s.default", "Default", "1", undefined, 1 more… ]

[ "[Profile1]
Name=cleanER
IsRelative=1
Path=Profiles/k46wtieb.clean", "Name", "cleanER", "IsRelative", "1", "Path", "Profiles/k46wtieb.clean", undefined, undefined, undefined, 1 more… ]

我不想在最后出现那些不必要的未定义,并且此模式仅限于有多少个 (?:\s+?([\S]+)=([\S]+)) ?我粘贴在模式的末尾

最佳答案

LIVE DEMO

JavaScript 代码

string = string.split(/\n\n/g); // split along the double newline to get blocks

var matches = [];  // the complete matches array has `match1`, `match2`, etc.

string.forEach(function(elem, index){ // for each block
  var matchArr = [];               // make a matchArr

  matchArr.push(elem); // wholeMatch 

  elem.replace(/\[([a-z0-9]+)\]/i, function(match, $1){    
    matchArr.push($1); // get the title  
  });

  elem.replace(/([a-z]+)=(.+)/ig, function(match, $1, $2){
    matchArr.push($1); // property
    matchArr.push($2); // value
  });

  matches.push(matchArr);  // push the `match` in bigger `matches` array
});

console.log(matches); // get the whole matches array

// You can use `matches[0]` to get the 1st match, and so on.

希望有帮助!

关于javascript - 尝试在 JavaScript 正则表达式中重复捕获 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651116/

相关文章:

javascript - 如何使用正则表达式和条件替换文本?

Ruby 仅替换作为哈希传递的多个正则表达式的第一次出现

javascript - 匹配2个单词的正则表达式

javascript - 广播事件和 on 在 Angular js 中不起作用

c# - 如何用增量数字替换之前包含一些字符的唯一字符串

regex - 找到一行末尾的任意字母,删除换行符而不替换目标

javascript - 来自较大图像文件的质量更好的缩略图

javascript - 在 while 循环 javascript 中 promise

javascript - 如何在网络上存储 javascript 资源?

javascript - 是否可以使用 JavaScript API 将视频持续时间添加到 JWPlayer?