如何拆分和重建字符串?

时间:2015-08-02 21:17:35

标签: javascript arrays regex replace split

我有以下代码:

var input = "RE1467R31294998782";
var arr = input.split("");

执行后我得到:

R,E,1,4,6,7,R,3,1,2,9,4,9,9,8,7,8,2

之后会出现以下代码:

arr2[0] = arr[0]+arr[1]; 
arr2[1] = arr[2]+arr[3];
arr2[2] = arr[4]+arr[5]; 
arr2[3] = arr[6]+arr[7];
arr2[4] = arr[8]+arr[9]; 
arr2[5] = arr[10]+arr[11];
arr2[6] = arr[12]+arr[13]; 
arr2[7] = arr[14]+arr[15];
arr2[8] = arr[16]+arr[17];

结果是:

[RE,14,67,R3,12,94,99,87,82]

最终结果应如下:

RE-1467-R3-1294-99-8782

因此,每当字符串从-更改为alphanumeric or >94时,我都想添加numeric and <95

但我该怎么做?有什么想法吗?

感谢您帮助我。

编辑:字符串总是18个字符或11个字符长。

更多例子:

1ZA2666W6831071503 to 1ZA2-66-6W-6831071503

1Z80A3109303081604 to 1Z-80-A3-109303081604

1Z80A3109603089504 to 1Z-80-A3-10-96-0308-95-04

3 个答案:

答案 0 :(得分:2)

现在最可能的答案是:

var input = "RE1467R31294998782";
var arr = input.split(""), arr2 = [];
for(var i=0;i<Math.floor(input.length/2);i++){
    arr2[i] = arr[i*2]+arr[(i*2)+1];
}

var rebuilt = '';
for (var i = 0; i < arr2.length; i++) {
    rebuilt += arr2[i]+(i < arr2.length - 1 && (isUnit(arr2[i]) || isUnit(arr2[i + 1])))?'-':'';
}

function isUnit(part) {
    return String(part).match(/[A-Za-z]/g) || Number(part) > 94;
}

alert(rebuilt);

甚至更丑,但希望在其他例子中满足条件:

var input = "RE1467R31294998782";
var arr = input.split("");
var arr2 = [];
arr2[0] = arr[0]+arr[1]; 
arr2[1] = arr[2]+arr[3];
arr2[2] = arr[4]+arr[5]; 
arr2[3] = arr[6]+arr[7];
arr2[4] = arr[8]+arr[9]; 
arr2[5] = arr[10]+arr[11];
arr2[6] = arr[12]+arr[13]; 
arr2[7] = arr[14]+arr[15];
arr2[8] = arr[16]+arr[17];

var rebuilt = '';

for (var i = 0; i < arr2.length; i++) {
    var part = arr2[i],
        nextPart = arr2[i + 1];

    rebuilt += part;

    if (i < arr2.length - 1 && ((isAlpha(part) && !isAlpha(nextPart)) || (!isAlpha(part) &&  isAlpha(nextPart)) || isBigNum(part) || isBigNum(nextPart)))
        rebuilt += '-';
}

function isAlpha(part) {
    return String(part).match(/[A-Za-z]/g);
}

function isBigNum(part) {
    return Number(part) > 94;
}

alert(rebuilt);

(很抱歉我的上述代码的编辑器很糟糕。)

答案 1 :(得分:2)

不幸的是,在修正之后,这个替代方案看起来并不好看,但工作正常:

var m = 0; // 0 = init, -1 = alpha-num, 1 = numeric < 95, 2 = numeric > 94
"RE1467R31295998782".replace(/[A-Z0-9]{2}/g, function(a) {
  if (isNaN(a)) {
    if (m == 1 || m == 2)
      a = '-' + a;
    m = -1;
  } else {
    if (parseInt(a) > 94) {
      if (m != 0)
        a = '-' + a;
      m = 2;
    } else {
      if (m == 2 || m == -1)
        a = '-' + a;
      m = 1;
    }
  }
  return a;
});

jsfiddle

答案 2 :(得分:2)

你可以像这样干净地做到这一点

&#13;
&#13;
var inputStr = 'RE1467R31294998782';
var splitRegex = /(\w{2})/g;
var pieces = inputStr.match(splitRegex);
var finalStr = [];
var prevDuet = '';

if (pieces && pieces.length > 0) {
    pieces.forEach(function(currentDuet) {
        if (!prevDuet) {
            finalStr.push(currentDuet);
        } else if (currentDuet > 94) {
            finalStr.push('-' + currentDuet);
        } else if ((isNaN(prevDuet) && !isNaN(currentDuet)) ||
                   (!isNaN(prevDuet) && isNaN(currentDuet))) {
            finalStr.push('-' + currentDuet);
        } else if (prevDuet > 94) {
            finalStr.push('-' + currentDuet);
        } else {
            finalStr.push(currentDuet);
        }
        prevDuet = currentDuet;
    });
}

alert(finalStr.join(''));
&#13;
&#13;
&#13;