简体   繁体   English

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

[英]Split long string into text chunks with jQuery

I have a long string that needs to be sliced into separated chunks inside an array, with a predefined length limit the chunks. 我有一个长字符串需要切成数组内的单独块,预定义的长度限制块。 Some rules apply: 一些规则适用:

  1. If the limit cuts a word, then the word is separated for the next chunk. 如果限制削减了一个单词,那么该单词将被分开以用于下一个单词。
  2. Slices must be trimmed (no spaces at the beginning or end of the array item). 必须修剪切片(数组项的开头或结尾没有空格)。
  3. Special punctuation .,!? 特殊标点符号!,? should stay with the word, and not be sent to the next chunk. 应该留在这个词,而不是被发送到下一个块。

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. 您可以在这间客房内运营整个公园,最少有3天的工作人员。 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? 你知道有谁可以联网8台连接机器并调试200万行代码,以便我为这份工作买单吗? 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."] 结果与当前代码 [“我完全”,“在我的时间中没有得到赏识”,“。你可以运行这整个”,“从这个房间停放”,“最小的工作人员最多”,“3天。你认为“,”有点自动化是“,”sy?还是便宜?你知道“,”任何可以联网的人,“8个连接机器”,“调试200万行”,我的出价代码“ ,“为了这份工作?因为如果”,“我可以看到h”,“我试试。”]

...it should actually be: ...它实际上应该是:

["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."] [“我完全”,“在我的时间里没有得到赏识。”,“你可以运行这个整体”,“从这个房间停放”,“最少3”的工作人员,“几天。你认为那种”,“自动化很容易?“,”或便宜?你知道任何人“,”谁可以网络8“,”连接机器和“,”调试200万行“,”代码为我竞标“,这项工作?因为如果他“,”我想看到他“,”试试。“]

As you can see, I'm still having trouble with rules 2 and 3 . 如您所见,我仍然遇到第2和第3条规则的问题。

This is my current code (you can check the working demo in jsfiddle ): 这是我目前的代码(您可以在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);
    });
});

The html form for the demo: 演示的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>

Example for 25 characters max, you can use this pattern: 最多25个字符的示例,您可以使用此模式:

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

demo 演示

code example: 代码示例:

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);

Note: if you need to choose dynamically the max size, you must use the alternative syntax to define your RegExp object: 注意:如果需要动态选择最大大小,则必须使用备用语法来定义RegExp对象:

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

pattern details: 图案细节:

\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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM