1

I have a string that contains a list of elements, some of which are fixed and some of which are pickable. As an example let's say my string is a grocery list:

"strawberry, raspberry, $2{tomato, sauerkraut, beans},
 potato, $1{{apple, pear}, {orange, banana}}"

And I would like to end up with something like:

["strawberry",
 "raspberry",
 [2, "tomato", "sauerkraut", "beans"],
 "potato",
 [1, ["apple", "pear"], ["orange", "banana"]]

I have no idea how can I tackle the multiple choices here so I'm open to any and all suggestions. Also you can modify the "syntax" (eg "$[num of coices]{list}") and add/remove fluff, it doesn't really matter. The simpler the merrirer! :]

edit: I have bazillions of these lists in the above format and converting them by hand is not an option unless there is no other solution... :(

Thanks!

2
  • Would be lovely with a real life example and what it is used for and where the string comes from Commented Apr 6, 2011 at 12:48
  • It is for automating the character generation for a game where player's can chose (example) if they go to a military school or a civilian academy. Depending on which path they chose (and some other stuff) there are a number of possible outcomes each handing out fixed rewards and bunch of "pick X from" types. These rewards are pretty much stored in a single string at the moment. I'm hoping it is possible to split them somehow because reworking them is a major pita. Commented Apr 6, 2011 at 12:56

2 Answers 2

3

Why are't you using JSON for this?

var list = ["strawberry",
     "raspberry",
     [2, "tomato", "sauerkraut", "beans"],
     "potato",
     [1, ["apple", "pear"], ["orange", "banana"]]];

var str = JSON.stringify(list);
/*
str = '["strawberry","raspberry",[2,"tomato","sauerkraut","beans"],"potato",[1,["apple","pear"],["orange","banana"]]]'
*/

var list2 = JSON.parse(str);
/*
list2 = ["strawberry",
         "raspberry",
         [2,"tomato","sauerkraut","beans"],
         "potato",
         [1, ["apple","pear"], ["orange","banana"]]]
*/
Sign up to request clarification or add additional context in comments.

5 Comments

Also possible with jQuery $.parseJSON - test case
Thanks but I forgot to mention I have literally thousands of these lists and converting them by hand is something I would like to avoid.
@Kenny: your question said 'Also you can modify the "syntax"' - and JSON is the standard format of serialized JavaScript objects.
@Kenny: where did these "thousands of lists" come from in the first place?
A friend of mine started making this thingie and spent ages typing it in. In that exact format. I took over thinking "oh, it only needs a few hundred lines of script".. Yeah, sure. :]]]
1

First: I strongly suggest using the JSON based solution that was mentioned by Matt. But for some reason you can't adapt your current code for that, then try this: Note: I haven't tested it for different test cases, but I think it will give you the idea...

  var testString = "strawberry, raspberry, $2{tomato, sauerkraut, beans},  potato, $1{{apple, pear}, {orange, banana}}";
    testString = testString.replace(/\$(\d+){/g, "{$1,");
    testString = testString.replace(/{/g, "[").replace(/}/g, "]");
    testString = testString.replace(/\b/g,'"')
    testString = testString.replace(/(")(\[)/g,'$2$1')
    testString = testString.replace(/(\])(")/g,'$2$1')
    testString = '[' + testString + ']';
    var obj = JSON.parse(testString);
    for(var o in obj)
    {
        alert(typeof obj[o]);
    }

Working Example: http://jsfiddle.net/PcMhw/4/

4 Comments

Thank you! This is exactly what I needed, works like charm! You, dear Sir (or Lady), are simply amazing.
Looks good to me. I'd take this code and use it to turn the data into JSON ASAP.
@Kenny: But be aware that the data in the string can break the code above easily. Like a space in the strwawberry (straw berry). Ofcourse you can enhance the code to make it handle those scenarios, but it gets more complex as you identify more test cases. That's why even I am inclined towards the JSON based solution. Just want to make sure you understand the risk.
Since it's only for a hobby I'm not really worried about breaking anything. :] Probably I'll do what Matt suggests and use this to convert the whole stuff into JSON in the first place. Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.