用双方括号括起来的任何东西的正则表达式

I have been trying to extract some strings or any kinds of values which are enclosed in double squar bracket. i.e. [[hello world]] or [[12_ nine]] etc. That's mean anything but which are enclosed in two squar bracket. Please check this URL, where I tried. However, I am explaining below what I did:

/\[[^\]]*\]]/

This pattern can validate anything inside [[]]. My problem is, it validate also []]. I am giving two examples what this parttern validate [[Welcome]] [v2.0]]. I need second example should not be validated. See the URL, you can understand better.

You need this:

/\[\[[^\]]*\]\]/

Here's how it's defined:

  • First two (escaped) brackets: \[\[
  • Then something that's not brackets: [^\]]*
  • Then two closing brackets: \]\] (you could keep them unescaped too, it's a matter of style)

Note that it won't match strings having square brackets in the middle (e.g. [[ A [B] C]]). If you want to allow those strings, use

/\[\[.*?\]\]/

If that must be the whole string, as seems to be from your comment below, use

/^\[\[.*?\]\]$/      (if you want to allow brackets inside)
/^\[\[[^\]]*\]\]$/   (if you don't)

^ an $ are end of string anchors and let you test the whole string.

Try this regex

\[{2}[^\[\]]+\]{2}

try this Demo

Explanation

  1. \[{2} I want exactly [[

  2. \[{2} I want exactly ]]

  3. [^\[\]]+ I want anything that is not [ or ] repeated one or more times

and if you want to catch only between two brackets

(?<=[^\[]\[{2})[^\[\]]+(?=\]{2}(?!\]))

try this Demo