简体   繁体   English

如何在不分割单词的情况下将字符限制设置为100?

[英]How can I set a character limit of 100 without splitting words?

I want to cut a string every 100 characters without cutting up words. 我想每100个字符切一个字符串而不切掉单词。

var TmpArray=[];
var str = 'this string will be cut up after every 100 characters but it will cut into words';
str=str.replace(/[^a-z A-Z0-9]+/g, '');
str = str.replace(/\s{2,}/g, ' ');
var sp=(str.match(new RegExp(" ", "g")) || []).length;
var max=100;
//Spaces will be converted into %20 (later) so each space must count as 3 characters.
var FoundSpaces=sp*3;
var tmp=max-FoundSpaces;
var cut=str.match(new RegExp('.{1,'+tmp+'}', 'g'));
for (i = 0; i < cut.length; i++){
    TmpArray.push(cut[i]);
}
console.log(TmpArray);

Output: ["this string will be cut up after every 100 characters b", "ut it will cut into words"] 输出: ["this string will be cut up after every 100 characters b", "ut it will cut into words"]

So how can I prevent it from splitting words like it did? 那么如何防止它像以前那样分裂单词呢?

Interesting question. 有趣的问题。 I will propose one more implementation of how you can use just array methods, combination of split + reduce : 我将提出另一种实现方法,说明如何使用数组方法以及split + reduce组合:

 var str = 'This example of the string that we want to split by spaces only making sure that individual chunk is less or equal to specified number.'; // Split by spaces str.split(/\\s+/) // Then join words so that each string section is less then 40 .reduce(function(prev, curr) { if (prev.length && (prev[prev.length - 1] + ' ' + curr).length <= 40) { prev[prev.length - 1] += ' ' + curr; } else { prev.push(curr); } return prev; }, []) // Print for testting .forEach(function(str) { console.log(str + ' ' + str.length); }); 

For this example I set maximum length of 40 characters. 对于此示例,我将最大长度设置为40个字符。

Output: 输出:

This example of the string that we want 39
to split by spaces only making sure that 40
individual chunk is less or equal to 36
specified number. 17

One more demo: http://jsfiddle.net/9tgo6n1t/ 另一个演示: http : //jsfiddle.net/9tgo6n1t/

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

相关问题 如何在不影响使用 Nodejs 或 Javascript 的 HTML 标签的情况下从 HTML 中获取 100 到 200 个单词? - How can I get 100 to 200 words from HTML without affecting HTML tags using Nodejs or Javascript? 如何在Sweetalert中设置字符数限制? - How do I set a character limit in sweetalert? 拆分单词的第一个字符 - Splitting the first character of the words 当达到字符数限制而又不打断单词时,如何在字符串中添加换行符? - How to add line breaks in a string when a character limit is reached without breaking the words? 如何在textarea HTML中限制单词而不是字符 - How can I limit words, not characters, in textarea HTML 在这种情况下,如何在jquery中实现限制字符? - How can I achieve a limit character in jquery in this situation? 如何制作像Twitter一样突出显示字符限制的文本区域? - How can I make a textarea with character limit highlighting like Twitter? Javascript:在不分割单词的情况下插入第n个字符,并将nbsp添加到计数中 - Javascript: insert &nbsp; every nth character without splitting words and adding the nbsp into the count 我如何在没有javascript拆分功能的情况下将字符串拆分成单词 - How can i split a string into words without split function in javascript 使用正则表达式设置字符数限制 - Set character limit with regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM