Search code examples
javascriptregexgreedy

smallest match in regular expression


I have this kind of expression:

var string = [a][1] [b][2] [c][3] [d .-][] [e][4]

I woud like to match the fourth element [d .-][] which may contain any character (letters, numbers, punctuation, etc) within the first pair of bracket but the second pair of bracket remains empty. Other elements, for example, [a][1], may contain any character but they do have a number inside the second pair of brackets.

I tried this:

string.match(/\\[[^]+]\\[ ]/);

but it is too greedy.

Any help would be appreciated.


Solution

  • I woud like to match the fourth element [d .-][] which may contain any character (letters, numbers, punctuation, etc) within the first pair of bracket but the second pair of bracket remains empty

    string.match(/\[[^\]]*\]\[\]/)
    

    should do it.

    To break it down,

    \[ matches a literal left square bracket,

    [^\]]* matches any number of characters other than a right square bracket,

    \] matches a literal right square bracket, and

    \[\] matches the two character sequence [], square brackets with nothing in between.

    To answer your question about greediness though, you can make the greedy match [^]+ non-greedy by adding a question-mark: [^]+?. You should know though that [^] does not work in IE. To match any UTF-16 code-unit I tend to use [\s\S] which is a bit more verbose but works on all browsers.