【发布时间】:2019-11-04 20:58:51
【问题描述】:
我正在处理的代码要求我获取字符串中的所有字符,将其分成行,然后将所有列记录回单个字符串。
我已将字符分成几行,但我无法将这些列重新记录到新行中:
let String= nodeStack.nodeValue;
// this wiil structure the text into a 8 columnns
let el=String.match(/.{1,8}/g);
for (i in el){
let node = document.createTextNode(el[i]);
let paraStack = document.createElement('p');
paraStack.appendChild(node);
stackCard.appendChild(paraStack);
//this is to log all the columns into a single row
//this is the part i am having issues with
let res = node.nodeValue;
let codMsg= [];
res.split('\n').forEach(function(v){
v.split('').forEach(function(v1, i){
codMsg[i] = (codMsg [i]|| '') + v1;
});
});
console.log(codMsg.join('\n')) ;
}
当前结果显示如下:
// console.log(el) gives
hertijhp
joiunjdk
njjooool
// console.log(codMsg.join('\n')) logs everything like this
h
e
r
t
i...
// instead of "hjneojrij"
【问题讨论】:
-
欢迎来到 SO!请为您的示例提供输入
-
您可以使用任何输入值进行测试
-
其实我做不到,就像
stackCard is not defined。我想说的是,您的代码应该是可重现的,以便其他 SO 用户对其进行检查 -
阅读后请检查问题中的代码:How to create a Minimal, Reproducible Example.
标签: javascript html arrays regex string