我尝试将长字符串分成多行以在react中输出:
let text ='fooooooooooooooooooooooooooooooooooooooooooooooooooo'
let rowEnd=10;
let regxp = new RegExp(rowEnd, "g");
let lines = text.match(regxp);
text = lines.join("\n");
console.log(text);
但是出现错误:TypeError:无法读取null的属性“ join”。
我在做什么错了?
答案 0 :(得分:1)
您需要使用正确的RegExp
/.{10}/g
let text = "fooooooooooooooooooooooooooooooooooooooooooooooooooo";
let rowEnd = 10;
let regxp = new RegExp(`.{${rowEnd}}`, "g");
let lines = text.match(regxp);
text = lines.join("\n");
console.log(text);
答案 1 :(得分:0)
另一种方法是使用replace
方法,例如:
let text ='0123456789abcsdbgdjb9876543210pol' ,
rowEnd = 10 ,
patt = new RegExp('.{' + rowEnd + '}','g') ;
console.log( text.replace(patt,'$&\n') ) ;