这应该与您正在寻找的完全匹配。在每行的非全局级别上使用它解析时 - 它将解析括号。
(?:\() #Non-Capture Group Parenthesis - for advanced submatching regex.
( # Start Capture Group 1
(?!\)) # Negative Lookahead
.*? # Match all characters except line break + Lazy
)? # End Capture Group 1 + Lazy (empty parenthesis)
(?:\)) #Non-Capture Group Parenthesis - for advanced submatching regex.
见下文...
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)
)
//CONSOLE OUTPUT
(3) ["(The Extremes of Good and Evil)", "()", "( )"]
0: "(The Extremes of Good and Evil)"
1: "()"
2: "( )"
length: 3