按字符数隔离字符串的值

时间:2015-02-28 22:12:01

标签: javascript jquery

如何用字符数(8)隔离字符串的值(00010111001101000001011100011001)使用jquery和javascript?

我想要这个: 结果:[ “00010111”, “00110100”, “00010111”, “00011001” ]

2 个答案:

答案 0 :(得分:1)

您可以使用.match(使用regexp \d{8}/g)函数返回所有characters(8)

console.log("00010111001101000001011100011001".match(/\d{8}/g));

答案 1 :(得分:0)

使用substring

var s = "00010111001101000001011100011001"; 
var v = new Array();
for ( i = 0 ; i < s.length ; i+=8 ) {
    v[i/8] = s.substring(i,i+8);
}    
console.log(v);

输出:

["00010111", "00110100", "00010111", "00011001"]