gpt4 book ai didi

javascript - 使用 jQuery 将长字符串拆分为文本 block

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:13 25 4
gpt4 key购买 nike

我有一个很长的字符串,需要在数组中分割成单独的 block ,并使用预定义的长度限制 block 。一些规则适用:

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

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

...它应该实际上是:

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

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

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

最佳答案

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

请注意,(?=\s|$) 可以替换为 (?!\S)

关于javascript - 使用 jQuery 将长字符串拆分为文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28225522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com