简体   繁体   English

正则表达式只返回第一场比赛?

[英]Regex returning only first match?

I have a text file containing a lot of rows, I'm filtering them to find the content I need, like: 我有一个包含很多行的文本文件,我正在过滤它们以找到我需要的内容,例如:

HBIJB LX 0359 25Aug14 07:37

This is my Regular Expression: 这是我的正则表达式:

/(?:(HB...)\s(LX|WK)\s((?:[0-9]){1,4})\s((?:[0-9]){2}(?:[A-Z,a-z]){3}(?:[0-9]){2})\s((?:[0-9]){2}:(?:[0-9]){2}))/g

My problem is that it's returning only the first match in the file: 我的问题是它只返回文件中的第一个匹配:

[ 
  'HBIJB LX 0359 25Aug14 07:37',
  'HBIJB',
  'LX',
  '0359',
  '25Aug14',
  '07:37',
  index: 323,
  input: /* Omitted */
]

The response is perfect, but why am I getting only the first match, even if I used the global /g flag? 响应是完美的,但是为什么我只得到第一场比赛,即使我使用了global /g标志?

Edit: 编辑:

I'm using NodeJs, and the script is nothing fancy: 我正在使用NodeJs,脚本没什么特别的:

var regex = /(?:(HB...)\s(LX|WK)\s((?:[0-9]){1,4})\s((?:[0-9]){2}(?:[A-Z,a-z]){3}(?:[0-9]){2})\s((?:[0-9]){2}:(?:[0-9]){2}))/g
console.log(regex.exec(data));

Edit 2: 编辑2:

Here an extrapolation of the content I need to filter. 这里是我需要过滤的内容的推断。

Lorem Ipsum Ø HBIJB LX 0359 25Aug14 07:37 HBIPV LX 2092 25Aug14 09:09
Lorem Ø HBIJB LX 1404 25Aug14 09:59 HBIJB LX 1405 25Aug14 10:53

It works fine for me, 这对我来说可以,

> var s = "Lorem Ipsum Ø HBIJB LX 0359 25Aug14 07:37 HBIPV LX 2092 25Aug14 09:09\nLorem Ø HBIJB LX 1404 25Aug14 09:59 HBIJB LX 1405 25Aug14 10:53";
undefined
> console.log(s)
Lorem Ipsum Ø HBIJB LX 0359 25Aug14 07:37 HBIPV LX 2092 25Aug14 09:09
Lorem Ø HBIJB LX 1404 25Aug14 09:59 HBIJB LX 1405 25Aug14 10:53
undefined
> m = s.match((?:(HB...)\s(LX|WK)\s((?:[0-9]){1,4})\s((?:[0-9]){2}(?:[A-Z,a-z]){3}(?:[0-9]){2})\s((?:[0-9]){2}:(?:[0-9]){2})))
> m = s.match(/(?:(HB...)\s(LX|WK)\s((?:[0-9]){1,4})\s((?:[0-9]){2}(?:[A-Z,a-z]){3}(?:[0-9]){2})\s((?:[0-9]){2}:(?:[0-9]){2}))/g);
[ 'HBIJB LX 0359 25Aug14 07:37',
  'HBIPV LX 2092 25Aug14 09:09',
  'HBIJB LX 1404 25Aug14 09:59',
  'HBIJB LX 1405 25Aug14 10:53' ]

This is a clarification and expansion of the correct answer provided by Avinash Raj . 这是对Avinash Raj提供的正确答案的澄清和扩展。

The problem is that I'm using RegExp.prototype.exec() but for what I intend to do, I need to use String.prototype.match() first: 问题是我正在使用RegExp.prototype.exec()但是对于我打算做的事情,我需要先使用String.prototype.match()

var values = "Lorem Ipsum Ø HBIJB LX 0359 25Aug14 07:37 HBIPV LX 2092 25Aug14 09:09\nLorem Ø HBIJB LX 1404 25Aug14 09:59 HBIJB LX 1405 25Aug14 10:53";
values.match(/(?:(HB...)\s(LX|WK)\s((?:[0-9]){1,4})\s((?:[0-9]){2}(?:[A-Z,a-z]){3}(?:[0-9]){2})\s((?:[0-9]){2}:(?:[0-9]){2}))/g);

values.forEach(function(row) {
  var regex = /(?:(HB...)\s(LX|WK)\s((?:[0-9]){1,4})\s((?:[0-9]){2}(?:[A-Z,a-z]){3}(?:[0-9]){2})\s((?:[0-9]){2}:(?:[0-9]){2}))/g;
  var data = regex.exec(row));
}

This is because String.prototype.match() is returning an array containing every string matching the given RegExp, whereas RegExp.prototype.exec() is returning an array containing the groups matched by the given RegExp, if there is more than a string matching, only the first one is returned. 这是因为String.prototype.match()返回一个包含与给定RegExp匹配的每个字符串的数组,而RegExp.prototype.exec()返回一个包含给定RegExp匹配的组的数组(如果有多个字符串)匹配,只返回第一个。

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

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