1

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.

4
  • Javascript doesn't have a hash table, you are talking about object in your example. Commented Jul 19, 2012 at 6:15
  • 1
    Split on the carraige return, loop over the resulting array and split on the colon. You'll need to create an array of objects since you can't repeat property names if there are multiple records. Commented Jul 19, 2012 at 6:18
  • I suggest you format your input in a better way, like in JSON. That way, it will be easier to parse, rather than make a parser for something difficult to parse. Commented Jul 19, 2012 at 6:21
  • In this line: 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 or Name: John Doe, Birth Date: 12/12/1981 or Name: John Doe Birth, Date: 12/12/1981. Commented Jul 20, 2012 at 4:27

4 Answers 4

5

You can use two splits to first split it into items and then to split each item:

var results = {};
var x = "Name: John Doe\nAge: 30\nBirth Date: 12/12/1981";
var pieces = x.split("\n"), part;
for (var i = 0; i < pieces.length; i++) {
    part = pieces[i].split(":");
    results[part[0]] = part[1];
}

Working example: http://jsfiddle.net/jfriend00/kYwq6/

Depending upon exactly what you're doing, you may want to trim the whitespace off beginning and end of each key and value before putting it into the results object.

Sign up to request clarification or add additional context in comments.

6 Comments

This works only if the deliminators are on separate lines. There is not always a line break between them.
@ThomasB - what is the delimiters pattern then? You have to provide that info since we only have one data sample to examine. And, did you really wait 21 hours to respond?
Look at the edit up top. And didn't wait, first chance I had to look at it since I got in. Normally takes longer for a response. But I think you put me on the right track, just need to add an extra step.
@ThomasB - From your example without the carriage returns, I still can't tell how one tells where the data stops and the next label starts. I added a comment to your question with examples that cause a problem.
Hah! Figured out the extra step: jsfiddle.net/kYwq6/9 Of course it will be \n and not <br />
|
1

Lol, everyone else put in loops! I decided to go with a liitle bit of a challenge and tried to do a quick test to see how I can code without any whiles or fors while keeping your string input the way it is:

    var x = "Name: John Doe Age: 30 Birth Date: 12/12/1981";

    x = x.replace(/[\t]/g,'');  //replace all tabs returns
    x = x.replace(/[\n\r]/g, '');  //replace all carriage returns
    x = "{" + x.replace("Name:","'Name':'").replace("Age:","','Age':'").replace("Birth","','Birth").replace("Date:","Date':'") + "'}";

    var results =  eval('(' + x + ')');

    var name = trim(results.Name);
    var age = trim(results.Age);
    var bday = trim(results.BirthDate);

What I did here was convert the x into object key value pair. So it would look like this:

{'Name':' John Doe ','Age':' 30','Birth Date':' 12/12/1981'}

Then you simply just call results.key

Anyways, looks like you already found your answer before I had a chance to put this up.

Comments

0

If your input has carraige returns or line feeds, you can split first on that then on the colon:

var x = "Name: John Doe\nAge: 30\nBirth Date: 12/12/1981";

function foo(s) {
  var a = s.split(/\n|\r/);
  var i = a.length;
  var b, obj = {};

  while (i--) {
    b = a[i].split(/\s*:\s*/);
    obj[b[0]] = b[1];
  }
  return obj;
}

var o = foo(x);
alert(o.Name); // John Doe

Comments

0

Somthing like this:

    var str = "Name: John Doe\nAge: 30\nBirth Date: 12/12/1981"
    var obj = {};
    var parts = str.split(/\n|\r/);
    for( var key in parts ){
      var tmp = parts[key].split(': ');
       obj[tmp[0]] = tmp[1];
    }
    alert(obj.Name)

PS Sorry for doubling - can't delete message

Comments

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.