[英]Regex to get multiple capture groups from one generic regex
This is for JavaScript. 这是针对JavaScript的。
My Problem: 我的问题:
So I want a regex that can get multiple sub capture groups for a string. 所以我想要一个正则表达式,它可以为一个字符串获取多个子捕获组。 Right now my attempt only gets one, while I expect 3. Ideally, it should be varied in that it should be able to get 2 or 3 or more.
现在,我的尝试只有一次,而我的期望是3。理想情况下,应该有所不同,因为它应该能够获得2或3或更多。
My attempt 我的尝试
let characters = { some: { test: { var: "hi" } } } let b = document.getElementById("body"); let h = b.innerHTML; let regex = /(?:\\$\\{)([^.]*[.]?)*(?:\\})/; let match; match = regex.exec(h); console.log(match);
<div id="body"> ${some.test.var} </div>
What I want 我想要的是
let characters = { some: { test: { var: "hi" } } } let b = document.getElementById("body"); let h = b.innerHTML; let regex = /(?:\\$\\{)([^.]*)[.]([^.]*)[.]([^.]*)(?:\\})/; let match; match = regex.exec(h); console.log(match);
<div id="body"> ${some.test.var} </div>
It seems you want to use a single capturing group to obtain multiple groups in the resulting array. 看来你想使用一个捕获组得到所得的阵列中的多个组。 It is not possible with JavaScript regex.
JavaScript正则表达式是不可能的。 See Repeating a Capturing Group vs. Capturing a Repeated Group .
请参阅重复捕获组与捕获重复组 。 There will always be as many groups in the resulting array as there are capturing groups in the pattern (+1, the zeroth group containing the whole match).
结果数组中的组总是和模式中的捕获组一样多(+1,第零个包含整个匹配项的组)。 If you use
(\\w* )*
it will match 0+ word chars and then a space and place it into Group 1 buffer, then, if it finds another streak of word chars and a space, the regex engine will match this text portion, replace the contents inside Group 1, and will go on searching, etc. 如果使用
(\\w* )*
,它将匹配0+个单词字符,然后匹配一个空格并将其放入第1组缓冲区中,然后,如果找到另一个单词字符和空格,则正则表达式引擎将匹配此文本部分,替换组1中的内容,然后继续搜索等。
You need to match ${...}
substrings first (there may be several options here, BTW, I suggest /\\${(\\w+(?:\\.\\w+)*)}/g
to only match ${...}
that have word chars and .
inside, but it can also be /\\${([^}]+)}/g
) and then split with .
您需要首先匹配
${...}
子字符串(这里可能有多个选项,顺便说一句,我建议/\\${(\\w+(?:\\.\\w+)*)}/g
仅匹配${...}
里面有字词char和.
,但是也可以是/\\${([^}]+)}/g
),然后用分割.
: :
const regex = /\\${(\\w+(?:\\.\\w+)*)}/g; const str = `\\${some.test.var} \\${some.test.var.more.here}`; let m; while (m = regex.exec(str)) { console.log(m[1].split('.')); }
Note you have the whole match value in m[0]
. 注意,整个匹配值都在
m[0]
。
Pattern details : 图案细节 :
\\${
- a ${
literal substring \\${
- ${
文字子字符串 (\\w+(?:\\.\\w+)*)
- Group 1 capturing: (\\w+(?:\\.\\w+)*)
-组1捕获:
\\w+
- 1 or more word chars \\w+
-1个或多个字字符 (?:\\.\\w+)*
- zero or more sequences of: (?:\\.\\w+)*
-零个或多个序列:
\\.
- a dot \\w+
- 1 or more word chars \\w+
-1个或多个字字符 }
- a literal }
}
-文字}
If you want to simplify the pattern, use /\\${([.\\w]+)}/g
or /\\${([^}]+)}/g
. 如果要简化模式,请使用
/\\${([.\\w]+)}/g
或/\\${([^}]+)}/g
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.