gpt4 book ai didi

javascript - 匹配括号中是否包含文本 - 正则表达式

转载 作者:行者123 更新时间:2023-11-28 14:45:47 25 4
gpt4 key购买 nike

以下是我的文字 -

Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This should also match () and ( ).

我正在尝试匹配文本 -

  • (善与恶的极端)
  • ()
  • ( )

我的正则表达式 - \(.\) 不起作用。

我还尝试了 \(*\) ,它与 ()(), ) 匹配,并且(善恶之极))。让我知道我在这里做错了什么。

最佳答案

您需要一个量词*来匹配括号内的零个或多个字符。也使其变得懒惰 ?,因此只要到达第一个右括号 \(.*?\):

,它就会停止

var s = 'Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This should also match () and ( ).'

console.log(
s.match(/\(.*?\)/g)
)

关于javascript - 匹配括号中是否包含文本 - 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46393875/

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