samedi 21 mars 2015

Javascript nested square brackets in string


I am looking for an easier (and less hacky) way to get the substring of what is inside matching square brackets in a string. For example, lets say this is the string:



[ABC[D][E[FG]]HIJK[LMN]]OPQR[STUVW]XYZ


I want the substring:



ABC[D][E[FG]]HIJK[LMN]


Right now, I am looping through the string and counting the open and closed brackets, and when those numbers are the same, I take substring of the first open bracket and last closed bracket.


Is there an easier way to do this (ie with regex), so that I do need to loop through every character?





Aucun commentaire:

Enregistrer un commentaire