Javascript 如何在两个词之间表达所有的意思

Javascript 如何在两个词之间表达所有的意思,javascript,node.js,regex,Javascript,Node.js,Regex,这个问题是关于正则表达式的 我目前正在使用Node.js的子进程的execFile。 它返回一个字符串,我试图从多行字符串中获取一个名称数组,如下所示: name: Mike age: 11 name: Jake age: 20 name: Jack age: 10 我试过: const regex_name = /pool: (.*)\b/gm; let names = string.match(regex_name); console.log(na

这个问题是关于正则表达式的

我目前正在使用Node.js的子进程的execFile。 它返回一个字符串,我试图从多行字符串中获取一个名称数组,如下所示:

   name: Mike
   age: 11

   name: Jake
   age: 20

   name: Jack
   age: 10

我试过:

const regex_name = /pool: (.*)\b/gm;
let names = string.match(regex_name);
console.log(names); // returns [ 'name: Mike', 'name: Jake', 'name: Jack' ]
但我想要的是:

['Mike', 'Jake', 'Jack']
我应该在正则表达式中更改什么?

您可以:

let names = string.match(regex_name).map(n => n.replace('name: ',''));
您还可以使用matchAll并提取组:

const exp = new RegExp('name:\\s(.+)','g');
const matches = string.matchAll(exp);
const results = [];

for(const match of matches) {
  results.push(match[1]);
}
或在功能上:

Array.from(string.matchAll(exp)).map(match => match[1]);
对于旧版本的节点:

const exp = new RegExp('name:\\s(.+)','g');
const results = [];
let match = exp.exec(string);

while(match) {
  results.push(match[1]);
  match = exp.exec(string);
}
常量字符串=` 姓名:迈克 年龄:11 姓名:杰克 年龄:20 姓名:杰克 年龄:10 `; 让names=string.match/name:\s.+/g.mapn=>n.replace'name:',; console.logname; const exp=new RegExp'name:\\s.+','g'; const matches=string.matchAllexp; 常量结果=[]; forconst匹配{ 结果:pushmatch[1]; } 控制台。日志结果; console.logArray.fromstring.matchAllexp.mapmatch=>match[1]; //节点8更新 常量结果2=[]; 让match=exp.execstring; whilematch{ 结果2.pushmatch[1]; match=exp.execstring; } console.logresults2 你能不能:

let names = string.match(regex_name).map(n => n.replace('name: ',''));
您还可以使用matchAll并提取组:

const exp = new RegExp('name:\\s(.+)','g');
const matches = string.matchAll(exp);
const results = [];

for(const match of matches) {
  results.push(match[1]);
}
或在功能上:

Array.from(string.matchAll(exp)).map(match => match[1]);
对于旧版本的节点:

const exp = new RegExp('name:\\s(.+)','g');
const results = [];
let match = exp.exec(string);

while(match) {
  results.push(match[1]);
  match = exp.exec(string);
}
常量字符串=` 姓名:迈克 年龄:11 姓名:杰克 年龄:20 姓名:杰克 年龄:10 `; 让names=string.match/name:\s.+/g.mapn=>n.replace'name:',; console.logname; const exp=new RegExp'name:\\s.+','g'; const matches=string.matchAllexp; 常量结果=[]; forconst匹配{ 结果:pushmatch[1]; } 控制台。日志结果; console.logArray.fromstring.matchAllexp.mapmatch=>match[1]; //节点8更新 常量结果2=[]; 让match=exp.execstring; whilematch{ 结果2.pushmatch[1]; match=exp.execstring; } console.logresults2 您可以使用获取name:之后的文本,并删除未定义的值

var str=` 姓名:迈克 年龄:11 姓名:杰克 年龄:20 姓名:杰克 年龄:10 `; const regex_name=/.*\b/gm; 让name=str.matchregex_name; names=names.mapstr=>{ 如果str.includeName{ 返回str.split':'.pop.trim; } }.filteritem=>项; console.logname 您可以使用获取name:之后的文本,并删除未定义的值

var str=` 姓名:迈克 年龄:11 姓名:杰克 年龄:20 姓名:杰克 年龄:10 `; const regex_name=/.*\b/gm; 让name=str.matchregex_name; names=names.mapstr=>{ 如果str.includeName{ 返回str.split':'.pop.trim; } }.filteritem=>项;
console.logname;这真的很接近。为什么不一直走下去呢?很抱歉@tadman,但我需要更多的描述而不是^…这是一种常见的方法,可以让解析像YAML.load一样简单。。。在你的数据上。你基本上需要对它进行一点调整,所以如果你能控制输入格式,那么现在就很容易修复了。@Andy tadman的意思是,如果你的文件是标准的YAML文件,你应该选择YAML解析器,而不是RegexUnfortute,我包含的示例是虚构的,真实的示例有一个奇怪的缩进,我已经用它进行了测试。它给我带来了严重的缩进错误。我无法控制这个返回的字符串…这非常接近。为什么不一直走下去呢?很抱歉@tadman,但我需要更多的描述而不是^…这是一种常见的方法,可以让解析像YAML.load一样简单。。。在你的数据上。你基本上需要对它进行一点调整,所以如果你能控制输入格式,那么现在就很容易修复了。@Andy tadman的意思是,如果你的文件是标准的YAML文件,你应该选择YAML解析器,而不是RegexUnfortute,我包含的示例是虚构的,真实的示例有一个奇怪的缩进,我已经用它进行了测试。它给我带来了严重的缩进错误。我无法控制这个返回的字符串…我目前正在使用您的建议:let names=string.matchregex_name.mapn=>n.replace'name:,;。我无法使用matchAll,因为我使用的是节点8,不支持matchAll。你知道如何使用正则表达式来获取名称值吗?@Andy我在回答中添加了一种预匹配方法。我目前正在使用你的建议:让name=string.matchregex_name.mapn=>n.replace'name:',;。我无法使用matchAll,因为我使用的是节点8,不支持matchAll。你知道如何使用正则表达式来获取名称值吗?@Andy我在答案中添加了一种预匹配方法