javascript - 使用 jQuery 将长字符串拆分为文本 block

标签 javascript jquery regex string substr

我有一个长字符串,需要将其切成数组内的单独 block ,并使用预定义的长度限制 block 。一些规则适用:

  1. 如果限制剪切了一个单词,则该单词将被分隔到下一个 block 。
  2. 必须 trim 切片(数组项的开头或结尾处没有空格)。
  3. 特殊标点符号.,!?应该留在单词中,而不是被发送到下一个 block 。

Original text: I am totally unappreciated in my time. You can run this whole park from this room with minimal staff for up to 3 days. You think that kind of automation is easy? Or cheap? You know anybody who can network 8 connection machines and debug 2 million lines of code for what I bid for this job? Because if he can I'd like to see him try.

Result with current code ["I am totally", " unappreciated in my time", ". You can run this whole", " park from this room with", " minimal staff for up to ", "3 days. You think that", " kind of automation is ea", "sy? Or cheap? You know", " anybody who can network ", "8 connection machines", " and debug 2 million line", "s of code for what I bid", " for this job? Because if", " he can I'd like to see h", "im try."]

...它实际上应该是:

["I am totally", "unappreciated in my time.", "You can run this whole", "park from this room with", "minimal staff for up to 3", "days. You think that kind", "of automation is easy?", "Or cheap? You know anybody", "who can network 8", "connection machines and", "debug 2 million lines of", "code for what I bid for", "this job? Because if he", "can I'd like to see him", "try."]

如您所见,我在规则 23 方面仍然遇到问题。

这是我当前的代码(您可以检查 working demo in jsfiddle ):

function text_split(string, limit, pos, lines) {
    //variables
    if(!pos) pos = 0;
    if(!lines) lines = [];
    var length = string.val().length;
    var length_current;

    //cut string
    var split = string.val().substr(pos, limit);
    if(/^\S/.test(string.val().substr(pos, limit))) {
        //check if it is cutting a word
        split = split.replace(/\s+\S*$/, "");
    }

    //current string length
    length_current = split.length;

    //current position
    pos_current = length_current + pos;

    //what to do
    if(pos_current < length) {
        lines.push(split);
        return text_split(string, limit, pos_current, lines);
    } else {
        console.log(lines);
        return lines;
    }
}
$(function(){
    $('#button').click(function(){
        text_split($('#textarea'), 25);
    });
});

演示的 html 表单:

<textarea id="textarea" rows="10" cols="80">I am totally unappreciated in my time. You can run this whole park from this room with minimal staff for up to 3 days. You think that kind of automation is easy? Or cheap? You know anybody who can network 8 connection machines and debug 2 million lines of code for what I bid for this job? Because if he can I'd like to see him try.</textarea>
<button id="button">demo</button>

最佳答案

示例最多 25 个字符,您可以使用以下模式:

/\S[\s\S]{0,23}\S(?=\s|$)/g

demo

代码示例:

var text = " I am totally unappreciated in my time. You can run this whole park from this room with minimal staff for up to 3 days. You think that kind of automation is easy? Or cheap? You know anybody who can network 8 connection machines and debug 2 million lines of code for what I bid for this job? Because if he can I'd like to see him try.";

var myRe = /\S[\s\S]{0,23}\S(?=\s|$)/g;
var m;
var result = new Array();

while ((m = myRe.exec(text)) !== null) {
   result.push(m[0]);
}
    
console.log(result);

注意:如果您需要动态选择最大大小,则必须使用替代语法来定义 RegExp 对象:

var n = 25;
var myRe = new RegExp("\\S[\\s\\S]{0," + (n-2) + "}\\S(?=\\s|$)", "g");

图案详细信息:

\S             # a non-space character (it is obviously preceded by a space 
               # or the start of the string since the previous match
               # ends before a space)

[\s\S]{0,23}   # between 0 or 23 characters

\S(?=\s|$)     # a non-space character followed by a space or the end of the string

请注意,(?=\s|$) 可以替换为 (?!\S)

关于javascript - 使用 jQuery 将长字符串拆分为文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28225522/

相关文章:

javascript - 将自定义属性附加到 DOM 节点

javascript - 在 VSCode 中进行 Firefox 插件开发的 Intellisense

javascript - 我们如何从 1 :Array(10) 获取数据

javascript - 如何将 Laravel 表中的多个复选框保存到数据库中?

java - 替换包含正则表达式的行

javascript - 以 mm/dd/yyyy 格式验证日期的正则表达式

javascript - 如何识别 Node.js 中的事件是从哪个流发出的

javascript - onclick 和 ng-click 事件不起作用

javascript - 在图表之间切换

PHP:正则表达式匹配字符串前面没有数字或空格