使用 jQuery 将长字符串拆分为文本块

Split long string into text chunks with jQuery

本文关键字:文本 拆分 字符串 jQuery 使用      更新时间:2023-09-26

我有一个长字符串,需要在数组中切成单独的块,预定义的长度限制了块。一些规则适用:

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

原文:我那个时代完全不被赏识。您可以在这个房间以最少的工作人员运行整个公园长达 3 天。你认为这种自动化很容易吗?还是便宜?你知道有人可以联网 8 台连接机器并为我竞标的这份工作调试 200 万行代码吗?因为如果可以的话,我希望看到他尝试。

当前代码的结果 ["我完全"、"在我的时代不受赞赏"、"。你可以运行整个","从这个房间停车","最少的工作人员最多","3天。你认为","自动化是ea","sy?还是便宜?你知道吗","任何可以联网的人","8台连接机","并调试200万行","我出价的代码","对于这项工作?因为如果","他可以我想看h","我试试。

。它实际上应该是:

["我完全","在我的时代不被欣赏.","你可以运行整个","从这个房间停车","最少的工作人员最多3","天。你认为那种","自动化很容易?","还是便宜?你知道任何人","谁可以联网8","连接机器和","调试200万行","我竞标的代码","这份工作?因为如果他","我想见他吗","试试。

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

这是我当前的代码(您可以在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

演示

代码示例:

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)