tags:

views:

74

answers:

4

I have a string like this

4741:GREEN,CIRHOSIS,ORANGE,Long-term,GREEN,HIS B CHIC,4642:GREEN,CRHOSIS,GREEN,HSysk B CC,

the sting contains two records with record ID 4741 and 4642 separated by character. Also within the records everything else is separated by comma(,)

how can I split this sting. Please note that this example string contains only 2 records but the other ones may contain more or less or none. Thank you for your help!

+3  A: 

Basic idea:

var str = "4741:GREEN,CIRHOSIS,ORANGE,Long-term,GREEN,HIS B CHIC,4642:GREEN,CRHOSIS,GREEN,HSysk B CC,1111:asdf"

var re = /(\d+):([^(\d+:)]+)/g;

var matches = str.match(re);

for(var x in matches){
    var parts = matches[x].split(":");
    var id = parts[0];
    var vals = parts[1].split(",");
    alert(id + "\n" + vals.length);
}
epascarello
can you tell me whats this doing var re = /(\d+):([^(\d+:)]+)/g;
@user295189: If I'm not mistaken: (\d+) matches a group containing 1 to N numbers, ([^(\d+:)]+) matches a group as long as you don't reach another sequence of numbers followed by a semi-colon.
haylem
A: 
function split(str) {
  var split = str.split(/,?(\w+):/);
  var result = {};
  for (var i = 1; i < split.length; i+=2) {
     result[split[i]] = split[i+1].split(/,/);
  }  
  return result;
}
split("4741:GREEN,CIRHOSIS,ORANGE,Long-term,GREEN,HIS B CHIC,4642:GREEN,CRHOSIS,GREEN,HSysk B CC"); 
// => {'4642': ['GREEN', 'CRHOSIS', 'GREEN', 'HSysk B CC'], '4741': ['GREEN', 'CIRHOSIS', 'ORANGE', 'Long-term', 'GREEN', 'HIS B CHIC']}
ormuriauga
A: 

You could use the String.match function:

var records = "4741:GREEN,CIRHOSIS,ORANGE,Long-term,GREEN,HIS B CHIC,4642:GREEN,CRHOSIS,GREEN,HSysk B CC,"
var regex = /[0-9]+:([a-zA-Z0-9]*,)+/gi;
records.match(regex);

Result: ["4741:GREEN,CIRHOSIS,ORANGE,", "4642:GREEN,CRHOSIS,GREEN,"]

If you are looking for something more specific you will need to extend your question.

Another interesting thing to do may be to add braces, brackets, and quotes to the string, then JSON.parse() it.

Mike S
+2  A: 

Use this:

var source = "4741:GREEN,CIRHOSIS,ORANGE,Long-term,GREEN,HIS B CHIC,4642:GREEN,CRHOSIS,GREEN,HSysk B CC,4643:GREEN,CRHOSIS,GREEN,HSysk B CC,4644:GREEN,CRHOSIS,GREEN,HSysk B CC,4645:GREEN,CRHOSIS,GREEN,HSysk B CC,4646:GREEN,CRHOSIS,GREEN,HSysk B CC,";

var extractRecordsAsArray = function (source) {
  var records = [];

  if (source && source.split) { // is string
    var re      = new RegExp("[0-9]+:[^0-9:]+", "g");
    var entries = source.match(re);

    for (var i = 0, len = entries.length; i < len; i++) {
      var entry = entries[i].split(':');

      records.push([entry[0], entry[1].split(',')]);
    }
  }
  return (records);
};

console.log("records: %o", extractRecordsAsArray(source));

Or this if you prefer a map for storage:

var extractRecordsAsMap = function (source) {
  var records = {};

  if (source && source.split) { // is string
    var re      = new RegExp("[0-9]+:[^0-9:]+", "g");
    var entries = source.match(re);

    for (var i = 0, len = entries.length; i < len; i++) {
      var entry = entries[i].split(':');

      records[entry[0]] = entry[1].split(',');
    }
  }
  return (records);
};

console.log("records: %o", extractRecordsAsMap(source));   

Not guaranteed bullet-proof and to be the most efficient, but will achieve decent performance for fairly big strings and works fine.

haylem