使用jQuery将长字符串拆分为文本块 [英] Split long string into text chunks with jQuery

查看:138
本文介绍了使用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. 如果限制限制了一个单词,那么该单词将被下一个单词分隔.
  2. 必须修剪切片(数组项的开头或结尾不能有空格).
  3. 特殊标点符号.,!?应该与单词保持一致,而不是发送到下一个区块.

原始文本:我完全不了解我的时间.您可以用最少的人手在这间房间中经营整个公园,最多3天.您认为这种自动化很容易吗?还是便宜?您知道有人可以联网8台连接机器并调试200万行代码来完成我为此工作的竞标吗?因为如果他能,我想看看他试试.

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.

使用当前代码的结果 [我完全是",在我的时间中不为人所知",.您可以运行整个",在此房间内停放",最少的人员最多3天.您认为,"自动化是一种"sy"吗?还是便宜?您知道,"可以将," 8个连接机器",并调试200万行",我要出价的代码",这份工作吗?因为if,他可以让我看到h","im 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:

[我完全不知所措",在我的时间里不为人所知.",您可以运行整个过程",可以在这个房间里泊车",最少要配备3个人," ;天.您认为这种自动化很容易?"还是便宜"?您知道有人,谁可以联网8个",连接机器",调试200万行",我要出价的代码",这项工作吗?".因为如果他是我想见见他",尝试".]

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

如您所见,规则 2 3 仍然存在问题.

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

这是我当前的代码(您可以查看在jsfiddle中工作的演示<​​/a>):

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

该演示的html表单:

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>

推荐答案

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

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

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

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

模式详细信息:

\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

这篇关于使用jQuery将长字符串拆分为文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆