首页 \ 问答 \ 使用jQuery将长字符串拆分为文本块(Split long string into text chunks with jQuery)

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

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

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

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

结果与当前代码 [“我完全”,“在我的时间中不被赏识”,“。你可以运行这整个”,“从这个房间停放”,“最小的工作人员,最多”,“3天。你认为“,”有点自动化是“,”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>

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>

原文:https://stackoverflow.com/questions/28225522
更新时间:2023-12-21 20:12

最满意答案

只需使用vclick而不是tapclick 。 这是一个jQuery Mobile事件,可以解决移动和桌面问题,点击/点击不能在两个平台上运行。

工作示例: http//jsfiddle.net/2ckHr/9/

$(document).delegate("#submit", "vclick", function() {
    alert($("#flip-1").val());
});

Just use vclick instead of tap or click. It is a jQuery Mobile event that bridges a mobile and desktop problems with tap/click not working on both platforms.

Working example: http://jsfiddle.net/2ckHr/9/

$(document).delegate("#submit", "vclick", function() {
    alert($("#flip-1").val());
});

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)