javascript - Breaking a String into Chunks based on Pattern -
i have 1 string, looks this:
a[abcdefghi,2,3,jklmnopqr] the beginning "a" fixed , non-changing, content within brackets , can follow pattern. alphabetical string, possibly followed numbers separate commas or more strings and/or numbers.
i'd able break chunks of string , numbers follow until "]" or string met.
probably best explained through examples , expected ideal results:
a[abcdefghi] -> "abcdefghi" a[abcdefghi,2] -> "abcdefghi,2" a[abcdefghi,2,3,jklmnopqr] -> "abcdefghi,2,3" , "jklmnopqr" a[abcdefghi,2,3,jklmnopqr,stuvwxyz] -> "abcdefghi,2,3" , "jklmnopqr" , "stuvwxyz" a[abcdefghi,2,3,jklmnopqr,1,9,stuvwxyz] -> "abcdefghi,2,3" , "jklmnopqr,1,9" , "stuvwxyz" a[abcdefghi,1,jklmnopqr,2,stuvwxyz,3,4] -> "abcdefghi,1" , "jklmnopqr,2" , "stuvwxyz,3,4" ideally malformed string partially caught (but nice extra):
a[2,3,jklmnopqr,1,9,stuvwxyz] -> "jklmnopqr,1,9" , "stuvwxyz" i'm using javascript , realize regex won't bring me way solution i'd big help. alternative lot of manually string parsing can doesn't seem best answer.
advice, tips appreciated.
update: yes did mean alphametcial (a-za-z) instead of alphanumeric. edited reflect that. letting me know.
you'd want in 2 steps. first, match against:
a\[([^[\]]*)\] and extract group 1. that'll stuff in square brackets.
next, repeatedly match against:
[a-z]+(,[0-9]+)* that'll match things "abcdefghi,2,3". after first match you'll need see if next character comma , if skip on it. (btw: if meant alphanumeric rather alphabetic examples, use [a-z0-9]*[a-z][a-z0-9]* instead of [a-z]+.)
alternatively, split string on commas , reassemble word number groups.
Comments
Post a Comment