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

标签 javascript regex character-class

以下是我的文字 -

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/

相关文章:

javascript - 从谷歌地理编码结果中删除奇怪的日文字符

c# - 正则表达式不匹配新行之间的内容 (\r\n)

bash - 打印 POSIX 字符类

xml - 正则表达式\p{L} 和\p{N}

javascript - jQuery 模板不显示数据元素

javascript - .load() 后回调函数不执行点击

javascript - 如何在不更改 html 的情况下使用 jquery 删除 <textarea> 中内容之前的多余空格

javascript - 将全局 try/catch 添加到 JavaScript

regex - 围绕外部定界符拆分字符串,尊重字符组

python - 为什么 '[0-9]*' 与我的 Python 正则表达式中的 'abc' 不匹配,因为字符串中有零个或多个数字?