Javascript 正则表达式反向引用未填充所有捕获组

发布于 2024-12-08 21:39:13 字数 600 浏览 0 评论 0原文

这里很奇怪(或者可能不是),我试图通过Javascript正则表达式检索两个捕获组,第一组:一个或多个数字(0-9),第二组:一个或多个单词字符或连字符(AZ,0-9) ,-) 但由于某种原因我永远无法检索后一组。

请注意:我特意包含了交替 (|) 字符,因为我希望可能收到其中一个)

这是我正在使用的代码:

var subject = '#/34/test-data'
var myregexp = /#\/(\d+)|\/([\w-]+)/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
  console.log(match[1]); // returns '34' successfully
  console.log(match[2]); // undefined? should return 'test-data'
}

有趣的是正则表达式巴迪告诉我,我确实有两个捕获组,并且实际上在测试短语上正确地突出显示了它们。

这是我的 JavaScript 语法有问题吗?

Strange one here (or maybe not), I am attempting to retrieve two capturing groups via Javascript regex, first group: one or more digits (0-9), second group: one or more word characters or hyphens (A-Z, 0-9, -) but for some reason I never can retrieve the latter group.

Please note: I have purposely included the alternation (|) character as I wish to potentially receive one or the other)

This is the code I am using:

var subject = '#/34/test-data'
var myregexp = /#\/(\d+)|\/([\w-]+)/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
  console.log(match[1]); // returns '34' successfully
  console.log(match[2]); // undefined? should return 'test-data'
}

Funny thing is Regex Buddy tells me I do have two capturing groups and actually highlights them correctly on the test phrase.

Is this a problem in my JavaScript syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临风闻羌笛 2024-12-15 21:39:13

如果您将:

var myregexp = /#\/(\d+)|\/([\w-]+)/;

通过删除 | 交替元字符更改为:,

var myregexp = /#\/(\d+)\/([\w-]+)/;

它将匹配两个组。目前,您的正则表达式正在寻找 \d+[\w-]+,因此一旦它与第一组匹配,它就会停止,第二组将为空。如果删除 |,它会查找 \d+,后跟 /,然后是 [\w-]+所以它总是匹配两者或都不匹配。

编辑:
要匹配所有 #/34/test-data#/test-data#/34,您可以使用 #(?:\/(\d+))?\/([\w-]+) 代替。

If you change:

var myregexp = /#\/(\d+)|\/([\w-]+)/;

by removing the | alternation meta-character to just:

var myregexp = /#\/(\d+)\/([\w-]+)/;

it will then match both groups. At present, your regex is looking for either \d+ or [\w-]+ so once it matches the first group it stops and the second will be empty. If you remove |, it's looking for \d+ followed by /, followed by [\w-]+ so it will always match either both or none.

Edit:
To match on all of #/34/test-data, #/test-data or #/34, you can use #(?:\/(\d+))?\/([\w-]+) instead.

高速公鹿 2024-12-15 21:39:13

如果去掉“|”你得到了你想要的结果......这有帮助吗?

var subject = '#/34/test-data'
var myregexp = /#\/(\d+)\/([\w-]+)/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
  console.log(match[1]); // returns '34' successfully
  console.log(match[2]); // undefined? should return 'test-data'
}

快乐编码!

编辑

我认为你的问题是,由于你使用了“|”,你告诉JS捕获第一组或第二组,并且由于JS eval是懒惰的,当它发现第一组,它停在那里...通过从正则表达式中删除 OR 操作数,您会得到两个结果...(类似于 AND)。

If you remove the "|" you get the result you want... does this help?

var subject = '#/34/test-data'
var myregexp = /#\/(\d+)\/([\w-]+)/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
  console.log(match[1]); // returns '34' successfully
  console.log(match[2]); // undefined? should return 'test-data'
}

Happy coding!

Edit

I think your problem was, that since you were using the "|", you were telling JS to catch either the first group or the second one, and since JS eval is lazy, when it found the first group, it stopped there... By removing the OR operand from the RegExp, you get both results...(something like an AND).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文