按设置的数量拆分字符串,并为拆分分配变量

Splitting string by set amount, and assigning variables to the splits

本文关键字:拆分 分配 变量 字符串 设置      更新时间:2023-09-26

我有字符串"hello123",想把它分成"hell"和"o123",但我不知道怎么做。另外,我想将firstPart分配给等于"地狱",secondPart分配给等于"o123"。任何帮助都非常感谢。

Here is my code:
var firstPart;
var secondPart;
var str = "hello123"
alert(str.split(""));
var str = 'hello123';
var firstPart=str.substr(0,str.length/2);
var secondPart=str.substr(str.length/2);
var str = "hello123";
var firstPart = str.substr(4); // => "Tabs1"
var secondPart = str.substr(id.length - 4); // => "1"

我不建议这样做,为此使用窗口对象。

var str = 'hello123';
str.match(/.{4}/g).forEach(function (a) {
    window[a] = a;
});
document.write(hell + o123);

var str = 'hello12312312bhfbdhfbh13h123bkbkfbk123bk123bk213kj';
document.write(str.match(/.{4}/g));