正如标题所说,我有一个字符串,我想把它分成n个字符的片段。

例如:

var str = 'abcdefghijkl';

当n=3时,它会变成

var arr = ['abc','def','ghi','jkl'];

有办法做到这一点吗?


当前回答

function chunk(er){
return er.match(/.{1,75}/g).join('\n');
}

上面的函数是我用于Base64分块的。它将创建一个每75个字符的换行符。

其他回答

function str_split(string, length = 1) {
    if (0 >= length)
        length = 1;
    
    if (length == 1)
        return string.split('');

    var string_size = string.length;
    var result = [];

    for (let i = 0; i < string_size / length; i++)
        result[i] = string.substr(i * length, length);

    return result;
}

str_split(str, 3)

基准测试:http://jsben.ch/HkjlU(不同浏览器的结果不同)

结果(Chrome 104)

如果你真的需要坚持使用.split和/或.raplace,那么使用/(?<=^(?:.{3})+)(?!$)/g

.split:

var arr = str.split( /(?<=^(?:.{3})+)(?!$)/ )
// [ 'abc', 'def', 'ghi', 'jkl' ]

.replace:

var replaced = str.replace( /(?<=^(?:.{3})+)(?!$)/g, ' || ' )
// 'abc || def || ghi || jkl'

/(?!$)/表示在字符串结束时不停止。没有它的:

var arr = str.split( /(?<=^(?:.{3})+)/ )
// [ 'abc', 'def', 'ghi', 'jkl' ] // is fine
var replaced = str.replace( /(?<=^(.{3})+)/g, ' || ')
// 'abc || def || ghi || jkl || ' // not fine

忽略组/(?:…)/是为了防止数组中存在重复项。没有它的:

var arr = str.split( /(?<=^(.{3})+)(?!$)/ )
// [ 'abc', 'abc', 'def', 'abc', 'ghi', 'abc', 'jkl' ] // not fine
var replaced = str.replace( /(?<=^(.{3})+)(?!$)/g, ' || ' )
// 'abc || def || ghi || jkl' // is fine

基于之前对这个问题的回答;下面的函数将分割一个字符串(str) n-number (size)的字符。

function chunk(str, size) {
    return str.match(new RegExp('.{1,' + size + '}', 'g'));
}

Demo

(function() { function chunk(str, size) { return str.match(new RegExp('.{1,' + size + '}', 'g')); } var str = 'HELLO WORLD'; println('Simple binary representation:'); println(chunk(textToBin(str), 8).join('\n')); println('\nNow for something crazy:'); println(chunk(textToHex(str, 4), 8).map(function(h) { return '0x' + h }).join(' ')); // Utiliy functions, you can ignore these. function textToBin(text) { return textToBase(text, 2, 8); } function textToHex(t, w) { return pad(textToBase(t,16,2), roundUp(t.length, w)*2, '00'); } function pad(val, len, chr) { return (repeat(chr, len) + val).slice(-len); } function print(text) { document.getElementById('out').innerHTML += (text || ''); } function println(text) { print((text || '') + '\n'); } function repeat(chr, n) { return new Array(n + 1).join(chr); } function textToBase(text, radix, n) { return text.split('').reduce(function(result, chr) { return result + pad(chr.charCodeAt(0).toString(radix), n, '0'); }, ''); } function roundUp(numToRound, multiple) { if (multiple === 0) return numToRound; var remainder = numToRound % multiple; return remainder === 0 ? numToRound : numToRound + multiple - remainder; } }()); #out { white-space: pre; font-size: 0.8em; } <div id="out"></div>

var str = 'abcdefghijkl';
var res = str.match(/.../g)
console.log(res)

这里点的数量决定了你想在每个单词中包含多少文本。

我最喜欢的答案是gouder hicham的。但我稍微修改了一下,让它更有意义。

let myString = "Able was I ere I saw elba";

let splitString = [];
for (let i = 0; i < myString.length; i = i + 3) {
    splitString.push(myString.slice(i, i + 3));
}

console.log(splitString);

下面是该代码的一个功能化版本。


function stringSplitter(myString, chunkSize) {
    let splitString = [];
    for (let i = 0; i < myString.length; i = i + chunkSize) {
        splitString.push(myString.slice(i, i + chunkSize));
    }
    return splitString;
}

以及函数的用法:

let myString = "Able was I ere I saw elba";
let mySplitString = stringSplitter(myString, 3);
console.log(mySplitString);

结果是:

>(9) ['Abl', 'e w', 'as ', 'I e', 're ', 'I s', 'aw ', 'elb', 'a']