gpt4 book ai didi

javascript - 我需要在正则表达式 javascript 中获得第二组

转载 作者:行者123 更新时间:2023-11-30 13:56:51 24 4
gpt4 key购买 nike

我不知道如何在给定字符串的情况下获得第二组。现在I have Full Watch and Group 1(not completely)(?:<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"

谢谢

最佳答案

您的表达式只有一个匹配组,它匹配 () 中的所有内容,你只需要根据昏迷将它分成两组。

(?:<p>)?@preview\((.*?)(?:,\s*(.*?))?\)(?:<\/p>)?

我改变了(.*)(.*?)(?:,\s*(.*?))?

(.*?)非贪婪的所有选择器匹配所有的东西,非贪婪的让它在找到的第一个昏迷处停止

(?:,\s*(.*?))?非捕获组捕获前一组之后的所有内容,包括 ,使用 ? 将其标记为可选

(.*?)第二个非贪婪的所有选择器捕获 , 之后的所有内容排除任何空格

https://regex101.com/r/c5qOFn/1

关于javascript - 我需要在正则表达式 javascript 中获得第二组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57106805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com