使用 jQuery 将长字符串拆分为文本块
Split long string into text chunks with jQuery
我有一个长字符串,需要在数组中切成单独的块,预定义的长度限制了块。一些规则适用:
- 如果限制削减了一个单词,则该单词将分隔到下一个块。
- 必须修剪切片(数组项的开头或结尾没有空格)。
- 特殊的标点符号.,!?应该保留在单词中,而不是发送到下一个块。
原文:我那个时代完全不被赏识。您可以在这个房间以最少的工作人员运行整个公园长达 3 天。你认为这种自动化很容易吗?还是便宜?你知道有人可以联网 8 台连接机器并为我竞标的这份工作调试 200 万行代码吗?因为如果可以的话,我希望看到他尝试。
当前代码的结果 ["我完全"、"在我的时代不受赞赏"、"。你可以运行整个","从这个房间停车","最少的工作人员最多","3天。你认为","自动化是ea","sy?还是便宜?你知道吗","任何可以联网的人","8台连接机","并调试200万行","我出价的代码","对于这项工作?因为如果","他可以我想看h","我试试。
。它实际上应该是:
["我完全","在我的时代不被欣赏.","你可以运行整个","从这个房间停车","最少的工作人员最多3","天。你认为那种","自动化很容易?","还是便宜?你知道任何人","谁可以联网8","连接机器和","调试200万行","我竞标的代码","这份工作?因为如果他","我想见他吗","试试。
如您所见,我仍然在规则 2 和 3 方面遇到问题。
这是我当前的代码(您可以在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
演示
代码示例:
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)
。
相关文章:
- 拆分文本以每隔n个字符添加一行新行,并注意空格
- 拆分文本节点
- 如何使用tspan拆分Ext.draw.text中的长文本
- jQuery拆分文本
- 计算文本块中的字符数,并将其拆分为两列
- 编写一个代码来拆分字符串“test”,并将Name的值分配给文本框,将ok的值分配到复选框
- 使用 jQuery 将长字符串拆分为文本块
- 拆分襟翼文本动画 Jquery
- 正则表达式使用 match() 将长文本拆分为单独的句子
- 将文本框中用逗号分隔的员工ID拆分为数组,然后处理这些字符串
- 将文本区域拆分为较小的变量
- 将表中的值拆分为不同的文本输入
- JavaScript 拆分字符串 选定的文本删除
- 数组文本或字符串拆分
- 使用javascript将文本区域拆分为不同的文本框
- 更新引导拆分按钮文本并使用下拉按钮功能
- 将文本拆分为一些粗体和一些不在功能Javascript片段上
- 将文本拆分为相等长度的字符串,以保持单词的完整性
- Javascript函数将文本拆分为两个相同长度的字符串
- 在 JavaScript 中将文本拆分为数组