我不知道如何得到第二组的字符串。现在我有完整的手表和第一组(不完全)和(?:<p>)?@preview\((.*)\)(?:<\/p>)?
示例字符串:
@preview(example-component/example-component)
<p>@preview(example-component/example-component)</p>
@preview(example-component/example-component, title="sadad" text="asd")
@preview(example-component/example-component, title="sadad" text="asd" )全场比赛:
@preview(example-component/example-component)或
<p>@preview(example-component/example-component)</p>或
@preview(example-component/example-component, title="sadad" text="asd")第1组:
example-component/example-component第2组:
title="sadad" text="asd"谢谢
发布于 2019-07-19 06:44:31
您的表达式只有一个匹配组,它与()中的所有内容匹配,您只需要根据昏迷将其分成两个组。
(?:<p>)?@preview\((.*?)(?:,\s*(.*?))?\)(?:<\/p>)?我把(.*)改成了(.*?)(?:,\s*(.*?))?
(.*?)非贪婪的所有选择器匹配一切,非贪婪使它停止在它发现的第一次昏迷
(?:,\s*(.*?))?非捕获组捕获前一组之后的所有内容,包括使用?将其标记为可选的,
(.*?)第二次非贪婪所有选择器捕获,之后的所有内容,不包括任何空格
发布于 2019-07-19 06:39:46
你可以用
\(([^,)]+)(?:,\s*([^)]+))?再详细一点,这是
\( # match a "(" literally
([^,)]+) # not a comma nor a ) -> group 1
(?:,\s* # a non-capturing group, followed by whitespaces
([^)]+) # not a ) -> group 2
)? # thw whole term is optional在JavaScript中
let strings = ['@preview(example-component/example-component)',
'<p>@preview(example-component/example-component)</p>',
'@preview(example-component/example-component, title="sadad" text="asd")',
'@preview(example-component/example-component, title="sadad" text="asd" )'];
let rx = /\(([^,)]+)(?:,\s*([^)]+))?/;
strings.forEach(function(item) {
let m = item.match(rx);
if (typeof(m[2]) !== "undefined") {
console.log(m[2]);
}
});
https://stackoverflow.com/questions/57106805
复制相似问题