I'm trying to figure out how to split a string using regex while having the results placed into a hash table.
Example:
var x = "Name: John Doe
Age: 30
Birth Date: 12/12/1981";
var arr = x.split(/Name:|Age:|Birth Date:/);
However while I can spit a string the problem is I can't store the values into a useful format such as a hash etc. Since some of the information may not always be shown.
I would want the results to be something like:
var myHash = {}; // New object
myHash['Name'] = "John Doe";
myHash['Age'] = "30";
myHash['Birth Date'] = "12/12/1981";
Is there an easy why to do this?
Edit: The script is used to parse data from a generated report. And the format does not always have carriage returns. It's going to be used to automatically generate notices instead of hand typing everything.
Example:
Name: John Doe Birth Date: 12/12/1981
Age: 30
However, after seeing examples I may be able to do it if I first add a carriage return or other special character in front of the regex matches. Need to figure out how to add a value in front of the regex matches first.
Name: John Doe Birth Date: 12/12/1981
, there is no way to tell where the name stops and the label for the next heading starts unless you can rely on multiple spaces or a carriage return. Without us knowing how the data can vary and what does not vary, we can't know what a valid algorithm is for a delimiter. For example, is the above supposed to be:Name: John, Doe Birth Date: 12/12/1981
orName: John Doe, Birth Date: 12/12/1981
orName: John Doe Birth, Date: 12/12/1981
. – jfriend00 Jul 20 '12 at 4:27