本文介绍了如何拆分和重建一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

  VAR输入=RE1467R31294998782;
变种ARR = input.split();

和执行它后,我得到:

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

这是去下code后:

  ARR2 [0] = ARR [0] + ARR [1];
ARR2 [1] = ARR [2] +改编[3];
ARR2 [2] =改编[4] +改编[5];
ARR2 [3] =改编[6] +改编[7];
ARR2 [4] =改编[8] +改编[9];
ARR2 [5] =改编[10] +改编[11];
ARR2 [6] =改编[12] +改编[13];
ARR2 [7] =改编[14] +改编[15];
ARR2 [8] =改编[16] +改编[17];

和其结果是:

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

最终的结果应该是如下:

RE-1467-R3-1294-99-8782

所以,我想补充一个 - 每次从字符串修改字母或> 94 数字和< 95

但我怎么做呢?任何想法?

谢谢你帮我。

编辑:字符串始终是18个字符或11个字符长

更多的例子:

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

1Z80A3109303081604到1Z-80-A3-109303081604

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


解决方案

您可以稍微干净不喜欢这样

\r
\r

VAR inputStr ='RE1467R31294998782';\r
变种splitRegex ​​= /(\\ W {2})/克;\r
VAR件= inputStr.match(splitRegex);\r
变种finalStr = [];\r
VAR prevDuet ='';\r
\r
如果(件&放大器;&放大器; pieces.length大于0){\r
    pieces.forEach(功能(currentDuet){\r
        如果(!prevDuet){\r
            finalStr.push(currentDuet);\r
        }否则如果(currentDuet> 94){\r
            finalStr.push(' - '+ currentDuet);\r
        }否则如果((isNaN(prevDuet)及&放大器;!isNaN(currentDuet))||\r
                   (isNaN(prevDuet)及!&安培; isNaN(currentDuet))){\r
            finalStr.push(' - '+ currentDuet);\r
        }否则如果(prevDuet> 94){\r
            finalStr.push(' - '+ currentDuet);\r
        }其他{\r
            finalStr.push(currentDuet);\r
        }\r
        prevDuet = currentDuet;\r
    });\r
}\r
\r
警报(finalStr.join(''));

\r

\r
\r

I have the following code:

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

and after executing it I get:

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

after that goes the following code:

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];

and the result is:

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

the end result should be the following:

RE-1467-R3-1294-99-8782

So, I would like to add a - every time the string changes from alphanumeric or >94 to numeric and <95.

But how do I do that? Any ideas?

Thanks for helping me out.

Edit: The string is always 18 chars or 11 chars long.

More examples:

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

1Z80A3109303081604 to 1Z-80-A3-109303081604

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

解决方案

You could somewhat cleanly do it like this

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(''));

这篇关于如何拆分和重建一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:00