Stephane
Stephane

Reputation: 5078

Extract string between brackets

I would like to extract characters between first opening and last closing brackets in a string like match[input[name="firstname"]] considering the characters to retrieve may also contain more brackets. In this case it would get input[name="firstname"] Also, the string may contain some special characters like { # / \ ^

Upvotes: 0

Views: 836

Answers (2)

georg
georg

Reputation: 214949

This somehow awkward-looking regular expression

/[^[\]]+\[[^[\]]+\]/

basically says "no brackets, then [, then no brackets, then ]".

s = 'match[input[name="firstname"]]'
> "match[input[name="firstname"]]"
re = /[^[\]]+\[[^[\]]+\]/
> /[^[\]]+\[[^[\]]+\]/
s.match(re)
> ["input[name="firstname"]"]

To make this slightly more useful, here's how to extract topmost brackets' content from a string with respect to nesting:

function extractTopmostBrackets(text) {
    var buf = '', all = [], depth = 0;
    text.match(/\]|\[|[^[\]]+/g).forEach(function(x) {
        if(x == '[')
            depth++;
        if(depth > 0)
            buf += x;
        if(x == ']')
            depth--;
        if(!depth && buf)
            all.push(buf), buf = '';
    })
    return all;
}

text = "foo [ begin [bar [baz] [spam]] end ] stuff [one [more]]"

console.log(extractTopmostBrackets(text))
// ["[ begin [bar [baz] [spam]] end ]", "[one [more]]"]

Recursive matches support in a regex engine would allow to write that in one line, but javascript re's are not that advanced.

Upvotes: 3

pyrrrat
pyrrrat

Reputation: 359

This will match everything between the first occurrence of [ and the last ] in your string, no matter what characters are in between:

> s = 'match[input[name="firstname"]]'
"match[input[name="firstname"]]"
> re = /\[(.*)\]/
/\[(.*)\]/
> q = s.match(re)
["[input[name="firstname"]]", "input[name="firstname"]"]
> q[1]
"input[name="firstname"]"

Upvotes: 1

Related Questions