Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个正则表达式与Golang和Python中的group 2匹配,而与JavaScript中的group 2不匹配?_Javascript_Regex - Fatal编程技术网

为什么这个正则表达式与Golang和Python中的group 2匹配,而与JavaScript中的group 2不匹配?

为什么这个正则表达式与Golang和Python中的group 2匹配,而与JavaScript中的group 2不匹配?,javascript,regex,Javascript,Regex,我使用正则表达式重复匹配节的第一部分和第二部分。它做得很好 我还希望它可以选择性地捕获第一部分中的最后一个实例,它正好是三位数。我不需要那个子部分的值。我只需要知道它在那里。因此,我使用匹配中的第一组和第三组,并测试第二组是否未定义 我遇到的问题是在JavaScript模式下,第二组结果总是未定义的 看看这个 在看到它在JavaScript中不起作用后,切换到任何其他模式(Golang、Python…),第一个匹配将为第二组提供“123”。这就是我希望它在JavaScript中所做的,但是,它没

我使用正则表达式重复匹配节的第一部分和第二部分。它做得很好

我还希望它可以选择性地捕获第一部分中的最后一个实例,它正好是三位数。我不需要那个子部分的值。我只需要知道它在那里。因此,我使用匹配中的第一组和第三组,并测试第二组是否未定义

我遇到的问题是在JavaScript模式下,第二组结果总是未定义的

看看这个

在看到它在JavaScript中不起作用后,切换到任何其他模式(Golang、Python…),第一个匹配将为第二组提供“123”。这就是我希望它在JavaScript中所做的,但是,它没有

那么,为什么它在JavaScript中不这样做呢?有没有办法用JavaScript生成正则表达式以产生所需的结果?

在JavaScript中尝试以下操作:

var regex = /((?:([0-9]{3})|[^,])+?)(?:,([^\.]*))?(?:\.|$)/g
  , string = 'some 123 thing, to test this with. which shows, not working in JS only'

console.log(regex.exec(string))

/* prints this:
[ 'some 123 thing, to test this with.',
  'some 123 thing',
  undefined, // <--------------------- I want this to be '123'
  ' to test with',
  index: 0,
  input: 'the whole string...'
] */

console.log(regex.exec(string))

/* prints this:
[ 'which shows, not working in JS only',
  'which shows',
  undefined, // this one is correct
  ' not working in JS only',
  index: 0,
  input: 'the whole string...'
] */

在非捕获组的每次迭代中,组1中的内容都被归零/重新填充,这就是ECMAScript正则表达式的工作方式。如果您的3位数区块正好出现在
,那么它会工作得很好,请尝试使用字符串测试您当前的正则表达式

您需要将第二个捕获组移出重复的非捕获组,如:

/([^,]*?(?:([0-9]{3})[^,]*)?)(?:,([^.]*))?(?:\.|$)/g
  ^^^^^^^^^^^^^^^^^^^^^^^^^^

现在,第一个捕获组模式匹配:

  • [^,]*?
    -除
    以外的任何0+字符尽可能少
  • (?:([0-9]{3})[^,]*)?
    -一次或零次出现:
    • ([0-9]{3})
      -第1组捕获3位数字
    • [^,]*
      -除
      之外,尽可能多的字符为零或更多

对于非捕获组的每次迭代,组1中的内容都会归零/重新填充,这就是ECMAScript正则表达式的工作方式。试着用一个像这样的标记重新编写模式。谢谢!这完美地解决了我的问题。我能够适应我正在做的复杂的事情,效果很好。
/([^,]*?(?:([0-9]{3})[^,]*)?)(?:,([^.]*))?(?:\.|$)/g
  ^^^^^^^^^^^^^^^^^^^^^^^^^^