javascript - How can I exclude "[" and "]" in a match like "[abc]"? -
i have following string:
[a] [abc] test [zzzz] i'm trying array so:
[0] => [1] => abc [2] => zzzz i've tried following code:
var string = '[a] [abc] test [zzzz]'; var matches = string.match(/\[(.*?)\]/g); for(var = 0; < matches.length; i++) console.log(matches[i]); but console output shows:
[a] [abc] [zzzz] i tried adding 2 non-capturing groups (?:), so:
var matches = string.match(/(?:\[)(.*?)(?:\])/g); but see same matches, unchanged.
what's going wrong, , how can array want?
match doesn't capture groups in global matches. made little helper purpose.
string.prototype.gmatch = function(regex) { var result = []; this.replace(regex, function() { var matches = [].slice.call(arguments,1,-2); result.push.apply(result, matches); }); return result; }; and use like:
var matches = string.gmatch(/\[(.*?)\])/g);
Comments
Post a Comment