javascript - 将 JavaScript(nodeJS) 字符串拆分为固定大小的 block

标签 javascript node.js

我想在 JavaScript 中使用空格作为分隔符(即它不应该打断单词)将一个字符串拆分成固定大小的 block (比如每个 140 个字符),注意:它应该处理换行符 目前我正在使用 wordwrap npm 包,但它不处理换行符。

var wrap = require('wordwrap')(140)    
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
console.log(wrap(str));

它适用于普通文本,但如果文本包含换行符,我会收到以下错误:
语法错误:意外的 token 非法
在 exports.runInThisContext (vm.js:73:16)
在 Module._compile (module.js:443:25)
在 Object.Module._extensions..js (module.js:478:10)
在 Module.load (module.js:355:32)
在 Function.Module._load (module.js:310:12)
在 Function.Module.runMain (module.js:501:10)
启动时 (node.js:129:16)
在 node.js:814:3

最佳答案

// define string variable
var string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"

function sliceMyString(str){
// initialize array (not required but verbose)
    var slices = [];

// while string is not empty
// take 140 characters
// check witch one was the last space or if the end of the line is reached
// then => push them in slices
// then => remove them from the string

    while(str != ''){
        var lastSpace = 0;

        for(var i = 0; i < str.length && i < 140; i ++){
            if(str[i] == ' '){
                lastSpace = i;
            }
            if(i == str.length - 1){
                lastSpace = str.length;
            }
        }

// insert into array (including trailing space, see below the codeblock)
        slices.push(str.slice(0, lastSpace));
        str = str.slice(lastSpace);
    }

// just logging the variables in the slices array

    slices.map(function(slice){
        console.log(slice);
    });
}

sliceMyString(string);

如果要去掉尾随空格,可以使用trim():

slices.push(str.slice(0, lastSpace).trim());

关于javascript - 将 JavaScript(nodeJS) 字符串拆分为固定大小的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630521/

相关文章:

javascript - 为什么我的 Javascript 变量没有初始化?

javascript - 将 jQuery 与 <select> 选项结合使用

javascript - React 组件,使用函数而不是内联 block 事件处理程序更好吗?

javascript - Node.js 基本问题

javascript - 如何使用nodejs运行coffeescript文件?

javascript - 我们可以从命令行从 flash 导出到 createjs 吗?

javascript - 如何获得所需的元素样式?

javascript - Unity3d : How to use dll from Javascript?

node.js - 使用 Node 将 token 保存在本地存储中

javascript - 可以在react-redux中编写props来覆盖connect()吗?