Is there a Regex-like that is capable of parsing matching symbols?

前端 未结 3 1350
天涯浪人
天涯浪人 2021-01-23 10:08

This regular expression

/\\(.*\\)/

won\'t match the matching parenthesis but the last parenthesis in the string. Is there a regular expression

3条回答
  •  星月不相逢
    2021-01-23 10:49

    Regular expressions are not powerful enough to find matching parentheses, because parentheses are nested structures. There exists a simple algorithm to find matching parentheses, though, which is described in this answer.

    If you are just trying to find the first right parenthesis in an expression, you should use a non-greedy matcher in your regex. In this case, the non-greedy version of your regex is the following:

    /\(.*?\)/
    

提交回复
热议问题