正如标题所说,我有一个字符串,我想把它分成n个字符的片段。
例如:
var str = 'abcdefghijkl';
当n=3时,它会变成
var arr = ['abc','def','ghi','jkl'];
有办法做到这一点吗?
正如标题所说,我有一个字符串,我想把它分成n个字符的片段。
例如:
var str = 'abcdefghijkl';
当n=3时,它会变成
var arr = ['abc','def','ghi','jkl'];
有办法做到这一点吗?
当前回答
我的解决方案(ES6语法):
const source = "8d7f66a9273fc766cd66d1d";
const target = [];
for (
const array = Array.from(source);
array.length;
target.push(array.splice(0,2).join(''), 2));
我们甚至可以这样创建一个函数:
function splitStringBySegmentLength(source, segmentLength) {
if (!segmentLength || segmentLength < 1) throw Error('Segment length must be defined and greater than/equal to 1');
const target = [];
for (
const array = Array.from(source);
array.length;
target.push(array.splice(0,segmentLength).join('')));
return target;
}
然后你可以以一种可重用的方式轻松地调用函数:
const source = "8d7f66a9273fc766cd66d1d";
const target = splitStringBySegmentLength(source, 2);
干杯
其他回答
如果你不想使用正则表达式…
var chunks = [];
for (var i = 0, charsLength = str.length; i < charsLength; i += 3) {
chunks.push(str.substring(i, i + 3));
}
jsFiddle。
...否则正则表达式的解决方案是相当好的:)
我的解决方案(ES6语法):
const source = "8d7f66a9273fc766cd66d1d";
const target = [];
for (
const array = Array.from(source);
array.length;
target.push(array.splice(0,2).join(''), 2));
我们甚至可以这样创建一个函数:
function splitStringBySegmentLength(source, segmentLength) {
if (!segmentLength || segmentLength < 1) throw Error('Segment length must be defined and greater than/equal to 1');
const target = [];
for (
const array = Array.from(source);
array.length;
target.push(array.splice(0,segmentLength).join('')));
return target;
}
然后你可以以一种可重用的方式轻松地调用函数:
const source = "8d7f66a9273fc766cd66d1d";
const target = splitStringBySegmentLength(source, 2);
干杯
这里有一种不需要正则表达式或显式循环的方法,尽管它有点扩展了一行代码的定义:
const input = 'abcdefghijlkm';
// Change `3` to the desired split length.
const output = input.split('').reduce((s, c) => {
let l = s.length-1;
(s[l] && s[l].length < 3) ? s[l] += c : s.push(c);
return s;
}, []);
console.log(output); // output: [ 'abc', 'def', 'ghi', 'jlk', 'm' ]
它的工作原理是将字符串分割为单个字符的数组,然后使用array。还原遍历每个字符。通常情况下,reduce会返回一个值,但在这种情况下,这个值恰好是一个数组,当我们传递每个字符时,我们将它附加到该数组的最后一项。一旦数组中的最后一项达到目标长度,就追加一个新的数组项。
var b1 = "";
function myFunction(n) {
if(str.length>=3){
var a = str.substring(0,n);
b1 += a+ "\n"
str = str.substring(n,str.length)
myFunction(n)
}
else{
if(str.length>0){
b1 += str
}
console.log(b1)
}
}
myFunction(4)
如果你真的需要坚持使用.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