问题标签 [capturing-group]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
124 浏览

javascript - Using two capturing groups with JavaScript's replace() function

Given such a string +as[23+"AS@D"-@] replace all instances of @ between the [ and ] with the number right after the [ (in this case: 23). Please answer using JavaScript (a suggestion is using .replace()). So using this algorithm, +as[23+"AS@D"-@] will be changed to +as[23+"AS23D"-23]

Here is my regex \[(\d+)[^\]]*(\@*)\] The second capturing group is not captured (Regexr tells me that nothing is captured within the second capturing group). I need help on making the second capturing group capture all instances of @

I have tried using Mozilla Developer for help, but it did not work. Any help will be appreciated. Answers have to have explanation.

If this question is not clear enough, please tell me in the comments how can this be improved.

0 投票
1 回答
85 浏览

javascript - 匹配不返回捕获组

我正在尝试将正则表达式应用于字符串,以搜索任何占位符,但我无法弄清楚为什么结果只是完全匹配,而不是捕获组。

»小提琴
(我使用了我的自定义函数write而不是console.log结果会显示在小提琴中)

为什么第三行未定义?我不明白!

确定表达是正确的。我有 1oo% 确定这是 JavaScript 的正确捕获组语法,它.match()首先返回一个完全匹配的数组,然后是所有捕获组。我什至用 regex101.com 对其进行了测试——在那里,我得到了捕获组。

这与其他问题不同,因为 OR 逻辑是问题的症结所在,而这里的管道|甚至没有出现。

0 投票
2 回答
59 浏览

regex - 如何仅查找和打印特定信息

我试图让 awk 只打印特定信息。当只涉及简单的文本字符串时,我可以做到。但是当我要求搜索和打印以下内容时它不起作用:

我正在寻找用点分隔的数字,就像 IP 地址一样。例如:

例如:

这仅打印TEXT并且工作正常。

这应该打印我需要的东西,但不起作用。

这工作正常,但打印整行,而不仅仅是我需要的:

我究竟做错了什么?

0 投票
1 回答
51 浏览

regex - 替换正则表达式模式中的单个术语

我在 Sphinx 中使用 regexp_filter 替换术语

在大多数情况下,我可以这样做,例如拼写错误很容易:

甚至使用捕获组表示法进行交换:

但是,在使用模式匹配来查找要替换的给定单词时,我遇到了更多麻烦:

其中 NewSearchTerm 将是我只想替换 \2 的术语(仅留下 \1 和模式的其余部分)。所以

因此,如果我当时有文字'Pizza and Taco Parlor'

将转换为'Pizza and Taco Store'

我知道在这种情况下 SearchTerm 是 /2 但不确定如何转换。我知道我可以附加 eg /2s 使其成为复数,但实际上我该如何替换它,因为它只是一个由多个捕获组组成的捕获组,而我只想替换该组?

0 投票
1 回答
40 浏览

javascript - 简单 `replace()` 的第二个参数的问题

我不明白,为什么这段代码不能正常工作?

它必须是在 camelCase 上交换 CSS 连字符语法的最简单解决方案。

0 投票
3 回答
1427 浏览

javascript - Javascript Regex 从每个换行条目中捕获多行内容

0 投票
5 回答
1091 浏览

javascript - 嵌套捕获组结果

我如何获得这样的数组

甚至更好

从字符串

依靠单个RegEx.exec()运行?

我试过这个:

但我无法获得子捕获组(如第一个示例)。

PS - 我想为两种类型的结果(平面数组嵌套数组)获取正则表达式

0 投票
0 回答
35 浏览

regex - 从各个地方捕获数据并将其放入一个命名组

我有一些我喜欢使用 Splunk 分析的数据。数据采用以下格式。

我需要使用正则表达式来命名组的各个部分客栈。

获取IP的示例,我有以下内容:

这给出了:

但是如何获取一个命名组的 URL,如下所示:

我认为在命名的捕获组中使用 捕获组,但这显然是不正确的:

不工作。

PS url 的长度可能不长,所以也应该处理。

0 投票
1 回答
164 浏览

regex - 将捕获的组分配给值,如果不匹配:分配字符串

在 Perl 正则表达式中,文档

... 在标量上下文中,$time =~ /(\d\d):(\d\d):(\d\d)/返回真或假值。但是,在列表上下文中,它返回匹配值的列表($1,$2,$3)

但是,当您提供替代选项时 - 找不到匹配项时 - 即使在列表上下文中也会分配 TRUE 或 FALSE 怎么办?

例如,我想将匹配的组分配给一个变量,如果找不到,则使用字符串值 ALL。

这可能吗?那么多个捕获的组呢?例如

where 如果第一组不匹配,'dit'则使用,如果第二组不匹配,则使用'dat'

0 投票
1 回答
646 浏览

regex - Is it possible to repeat a captured group in the substitution in regular expressions?

Is it possible with regex to repeat a captured group an amount of times with the added restriction that the amount to repeat is also found in a capture group?

Example:

I have never seen anything that does this, but maybe I have been looking in the wrong places. Note: I am using Perl - but I am interested to see answers for other flavours as well.