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

时间:2015-01-29 22:05:24

标签: javascript jquery regex string substr

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

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

    原文:我的时间完全没有受到重视。您可以在这间客房内运营整个公园,最少有3天的工作人员。您认为那种自动化很容易吗?还是便宜?你知道有谁可以联网8台连接机器并调试200万行代码,以便我为这份工作买单吗?因为如果他能够看到他尝试的话。

         

    结果显示当前代码 ["我完全是","在我的时间里没有得到赏识","。你可以运行这整个","这个房间有","最短的工作人员,最长可达"," 3天。你认为","有点自动化是ea"," sy?还是便宜?你知道","任何可以联网"," 8连接机器","并调试200万行"," s代码为我出价","这份工作?因为如果","我可以在h","我试试。"]

    ......它应该实际

      

    ["我完全","在我的时间里没有得到赏识。","你可以在这个房间里运行这整个","停车与","最少3到34天的员工,#34;天。你认为那种自动化很容易吗?","或便宜?你认识任何人","谁可以联网8","连接机器","调试200万行","代码用于什么我竞标","这份工作?因为如果他","我可以看到他","尝试。"]

    如你所见,我仍然遇到规则 2 3 的问题。

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

1 个答案:

答案 0 :(得分:5)

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