开发者

dice up a string in JS

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!


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);
}


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.


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']}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜