簡體   English   中英

javascript正則表達式最里面的括號不要用引號引起來

[英]javascript regex innermost parentheses not surrounded by quotes

我正在嘗試創建一個JS正則表達式,該表達式將查找沒有用引號引起來的最里面的括號。 括號可以無限嵌套。

范例1:

(This should not match (This will match "(with this)" intact), but will exclude this and (this))

因此,在示例中,結果將是This will match "(with this)" intact

范例2:

(This should not match (This will match with this intact), but will exclude this and (this))

因此,在本例中,結果將是This will match with this intact

我試過了/\\(([^\\(\\)]+)\\)/ ,這給了我最深的括號,但是沒有轉義引號。

如果您不介意自己剝掉外部括號

/\([^()"]*(?:"[^"]*"[^()"]*)*\)/

在第一個示例輸入中給我(This will match "(with this)" intact) ,在第二個示例中給我(This will match "(with this)" intact) (This will match with this intact)

這是一個可能的選擇:

/\(((?:"\(|\)"|[^()])+)\)/

用法示例:

'( (a"(b)") )'.match(/\(((?:"\(|\)"|[^()])+)\)/) // ["(a"(b)")", "a"(b)""]

說明:

"\(|\)"|[^()]   "( or )" or any char except parenthesis
((?:...)+)      one or more times (capture -> group 1)
\(...\)         enclosed in a pair of parenthesis

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM