我试图捕获具有以下形式的字符串中的模式:item(options)、item(options)等。 其中“item”是一个单词,选项可以包含任何字符。 项目可能没有选项,因此字符串可以是“item,item, item(options),item”。
到目前为止,我已经做到了:
/(\w+(\(.+\))*)(,(\w+(\(.+\))*))*/
但这并没有真正起作用。我希望能够捕获所有结果,以便我可以轻松访问项目和选项。任何帮助将不胜感激!
编辑: 恐怕这是不可能的:如果“选项”可以包含任何字符,例如“,”或“)”,那么就无法编写正确的正则表达式,可以吗?
最佳答案
怎么样:
/
\w+ : one or more word characters
(?: : start non capture group
\([^)]+\) : an open parens, some characters that are not parens, a close parens
)? : this group is optional
(?: : start non capture group
, : a comma
\w+
(?:
\([^)]+\)
)?
)* : 0 or more times the same pattern preceeded by a comma.
/x : allow comments in regex
关于javascript - 分隔值列表的正则表达式,其中值可能包含分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25287967/