I have a small piece of code that needs to split a string into a javascript object.
The resulting object need to look something like this:
var myObject = {
valueOne: value,
valueTwo: value,
arrayOfValues: [values]
}
The strings that need to be converted come in two different formats. Those formats look like:
1) "MyValues are ('1','2','3')"
–this would be translated into :
var myObject = {
valueOne: myValues,
valueTwo: are,
arrayOfValues: [1,2,3]
}
or
2) "MyValue="1""
–this would be translated into :
var myObject = {
valueOne: myValue,
valueTwo: =,
arrayOfValues: [1]
}
I know I can split this string like so:
var arr[] = myString.split(" ");
But deciding which splitter to use based on the makeup of the string, then converting it into a javascript object is a world unknown to me.
Thanks for your help.
EDIT — I made a small mistake with example number two
I updated it so that it’s correct. — The string I’m receiving is in the format of:
`"MyValue="1""`
samy-delux, thanks for your help! That works great, but the change to this second string breaks your example. (I’m sorry, I mistyped the format of the string.)
I’m doing my best to learn about regular expressions. But don’t know enough yet to solve this problem.
Here you go ( http://jsfiddle.net/VBmb8/ ):
* After your edit *
http://jsfiddle.net/VBmb8/2/