Split long string into text chunks with jQuery

眉间皱痕 提交于 2019-12-31 02:09:43

问题


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. 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."]

...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."]

As you can see, I'm still having trouble with rules 2 and 3.

This is my current code (you can check the 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);
    });
});

The html form for the demo:

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

回答1:


Example for 25 characters max, you can use this pattern:

/\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:

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


来源:https://stackoverflow.com/questions/28225522/split-long-string-into-text-chunks-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!