简体   繁体   English

在JavaScript中将字符串分解为最大字符部分

[英]Breaking Down a String into Maximum Character Sections in JavaScript

I need to break apart strings in JavaScript into chunks of no greater than 100 characters while maintaining breaks between words. 我需要将JavaScript中的字符串分成不超过100个字符的块,同时保持单词之间的间隔。 I have a function in my own personal library for chunkifying a string into 100-character sections, but I can't seem to wrap my head around how to adapt it to avoid splitting in the middle of a word. 我在自己的个人库中有一个函数,用于将一个字符串分块为100个字符的部分,但是我似乎无法绕过如何适应它的头,以免在单词中间分裂。 I figure something can be managed using regular expressions or something, but it just isn't coming to me. 我认为可以使用正则表达式或某些东西来管理某些东西,但是这并不是我想要的。 One caveat to any solution is that it has to be pure JavaScript, no jQuery, and the environment has no access to browser-related globals. 任何解决方案的一个警告是,它必须是纯JavaScript,没有jQuery,并且该环境无法访问与浏览器相关的全局变量。

-- EDIT -- -编辑-

Ok, I've written some code, but I'm getting strange results... 好的,我已经写了一些代码,但是结果却很奇怪。

function chunkify(str) {
    var wsRegEx = /\S/;
    var wsEndRegEx = /\s$/;
    var wsStartRegEx = /^\s/;
    var chunks = new Array();
    var startIndex = 0;
    var endIndex = 100;
    var totalChar = 0;
    while (true) {
        if (totalChar >= str.length) break;
        var chunk = str.substr(startIndex,endIndex-startIndex);
        while (wsStartRegEx.test(chunk)) {
            startIndex++;
            endIndex++;
            totalChar++;
            chunk = str.substr(startIndex,endIndex-startIndex);
        }
        if (!wsEndRegEx.test(chunk)) {
            while (wsRegEx.test(chunk.charAt(endIndex))) {
                endIndex--;
            }
            chunk = str.substr(startIndex,endIndex-startIndex);
        }
        chunks.push(chunk);
        totalChar += chunk.length;
        startIndex = endIndex;
        endIndex += 100;
    }
    return chunks;
}

A previous version I posted wasn't counting chunks correctly, but this version, which does seem to break correctly, is now breaking mid word. 我发布的先前版本无法正确计数数据块,但此版本似乎确实可以正确破解,但现在却打破了常规。

-- EDIT #2 -- -编辑#2-

I think I got it working great now. 我想我现在工作得很好。 This seems to do the trick: 这似乎可以解决问题:

function chunkify(str) {
    var wsRegEx = /\S/;
    var chunks = new Array();
    var startIndex = 0;
    var endIndex = 100;
    while (startIndex < str.length) {
        while (wsRegEx.test(str.charAt(endIndex))) {
            endIndex--;
        }
        if (!wsRegEx.test(str.charAt(startIndex)))
            startIndex++;
        chunks.push(str.substr(startIndex, endIndex - startIndex));
        startIndex = endIndex;
        endIndex += 100;
    }
    return chunks;
}

Is there a cleaner way to do this, or have I gotten this to be about as efficient as it'll get? 有没有更干净的方法可以做到这一点,或者我是否已经知道它会达到它所要达到的效率?

I have tried to spec this out for you, so you understand one way it can be done 我已尝试为您说明这一点,因此您了解可以完成此操作的一种方法

function chunkify (str) {
  var chunks = [];
  var startIdx = 0, endIdx;
  //Traverse through the string, 100 characters at a go
  //If the character in the string after the next 100 (str.charAt(x)) is not a whitespace char, try the previous character(s) until a whitespace character is found.
  //Split on the whitespace character and add it to chunks
  return chunks
}

这是使用正则表达式的一种方法:

chunks = str.match(/.{1,100}/g);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM