Skip to content

Instantly share code, notes, and snippets.

@hmasato
Created December 3, 2013 05:36
Show Gist options
  • Select an option

  • Save hmasato/7764385 to your computer and use it in GitHub Desktop.

Select an option

Save hmasato/7764385 to your computer and use it in GitHub Desktop.

Revisions

  1. hmasato created this gist Dec 3, 2013.
    43 changes: 43 additions & 0 deletions JS_seqParser.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    //--------------------------------
    // zero padding
    //--------------------------------
    function _padding(val,n)
    {
    n -= (''+val).length-1;
    return((n>0?(new Array(n)).join('0'):'')+val);
    }
    //--------------------------------
    // examples:
    // "c##(1,3)" or "c##(1:3)" or "c##(1-3)" -> ["c01", "c02", "c03"]
    // "c#(1,30,10)" -> ["c1", "c11", "c21"]
    // "s##(1,2)_c###(1,3)_A"
    // -> ["s01_c01_A", "s01_c02_A", "s01_c03_A", "s02_c01_A", "s02_c02_A", "s02_c03_A"]
    //--------------------------------
    function _seqParser(str)
    {
    var ret = [""];

    var p = 0;
    var re = RegExp(/(#+)\(([0-9][0-9 :,\-]*)\)/g);
    while(m = re.exec(str))
    {
    if(m.length < 3) continue;
    var pre = str.substring(p, m.index);
    var r = m[2].replace(/ /g, "").replace(/[,\-]/g, ":");
    r = r.split(":"); //string -> array
    var fs = parseInt(r[0]);
    var fe = r.length < 2 ? fs : parseInt(r[1]);
    var fb = r.length < 3 ? 1 : parseInt(r[2]);
    var buf = [];
    for(var j=0; j< ret.length; j++){
    for(var i=fs; i<= fe; i += fb){
    buf.push(ret[j] + pre + _padding(i, m[1].length));
    }
    }
    ret = buf;
    p = m.index + m[1].length + m[2].length + 2;
    }
    for(var j=0; j< ret.length; j++) ret[j] += str.substring(p);

    return(ret);
    }