Javascript – return string between square brackets

Use grouping. I’ve added a ? to make the matching “ungreedy”, as this is probably what you want.

var matches = mystring.match(/\[(.*?)\]/);

if (matches) {
    var submatch = matches[1];
}

Leave a Comment