regex - Regular expression to get string and number between square brackets - javascript -
i needing number (1) , string (start) string can increment number, below regex works still has [] around output - have regex remove []'s well?
var $name = 'event[1][start]'; var parts = $name.match(/\[(.*?)\]/g); console.log(parts[0] + ' - ' + parts[1]); // wish eventually... // var $newname = 'event[' + ++parts[0] + '][' + parts[1] + ']';
many thanks
if string in format something[part0][part1]
, use this:
var parts = $name.match(/.*?\[(.*?)\]\[(.*?)\]/); console.log(parts[1] + ' - ' + parts[2]);
when use global
flag, array of matches of entire regexp, not capture groups. in case don't seem need global, want capture 2 specific elements, use 2 capture groups.
note capture group numbering starts @ 1 -- parts[0]
match entire regep.
Comments
Post a Comment