Regular expression to get text between square brackets including disparity?

后端 未结 4 424
心在旅途
心在旅途 2021-01-29 12:31

I need to find a regular expression for use in C# (JavaScript as well), for get the text inside the either square brackets combination.

I try several ways, but I give up

4条回答
  •  心在旅途
    2021-01-29 12:50

    This matches everything except square brackets: [^\[\]]+

    This captures anything that is not a square bracket between any number of open (LHS) and close (RHS) square brackets:

    \[+([^\[\]]+)\]+

    Example usage in Javascript:

    '[[[[[test]]]]'.match(/\[+([^\[\]]+)\]+/)
    > ["[[[[[test]]]]", "test"]
    

    The regex tester at http://regexpal.com/ is useful for trying out regexes.

提交回复
热议问题