I have this string that I'm trying split into nested arrays:
x = '{type:paragaph|class:red|content:[class:intro|body:This is the introduction paragraph.][body:This is the second paragraph.]}'
x << '{type:image|class:grid|content:[id:1|title:image1][id:2|title:image2][id:3|title:image3]}'
This so far:
x.scan(/\{(.*?)\}/).map {|m| m[0].split(/\|\s*(?=[^\[\]]*(?:\[|$))/)}
successfully divides it into:
[
["type:paragaph", "class:red", "content:[class:intro|body:This is the introduction paragraph.][body:This is the second paragraph.]"],
["type:image", "class:grid", "content:[id:1|title:image1][id:2|title:image2][id:3|title:image3]"]
]
I'm trying to add an additional map:
x.scan(/\{(.*?)\}/).map {|m| m[0].split(/\|\s*(?=[^\[\]]*(?:\[|$))/)}.map {|m| m[0].split(/\:\s*(?=[^\[\]]*(?:\[|$))/)}
which returns:
[["type", "paragaph"], ["type", "image"]]
however I am after this instead:
[
[['type','paragaph'], ['class','red'], ['content',['[class:intro|body:This is the introduction paragraph.][body:This is the second paragraph.]']],
[['type','image'], ['class','grid'], ['content','[id:1|title:image1][id:2|title:image2][id:3|title:image3]']]
]
The map appears to be applying itself to the whole 2nd level array instead of each element inside it. What am I doing wrong here?