use*_*710 0 javascript string recursion
我需要逐行构建一个字符串数组.它就像wordwrap.
我会输入这样的文字:
var inputString = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration';
Run Code Online (Sandbox Code Playgroud)
我需要数组中的每一行最多38个字符.我不想在中间分割任何单词,所以如果38个字符位于单词的中间,则返回最近的空格字符.
期望的输出:
var output = [
'There are many variations of passages',
'of Lorem Ipsum available, but the',
'majority have suffered alteration'
];
Run Code Online (Sandbox Code Playgroud)
输出不正确:
'There are many variations of passages '
'of Lorem Ipsum available, but the majo'
'rity have suffered alteration.'
Run Code Online (Sandbox Code Playgroud)
我试图用空格字符拆分输入文本,最后得到:
var splitInput = [
'There',
'are',
'many'
...
]
function conc(arguments){
if (arguments.length === 0)
return "";
else
return arguments.shift() + conc(arguments);
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何检查参数是否总计38或更多,然后如果他们这样做则回溯.
你可以使用string.prototype.match()来做到这一点
var inputString = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration';
let result = inputString.match(/\b.{1,38}\b/g);
console.log(result);Run Code Online (Sandbox Code Playgroud)
由于正则表达式不重叠,您将获得所需的结果
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |