[英]Javascript: Cut string after last specific character
I'm doing some Javascript to cut strings into 140 characters, without breaking words and stuff, but now i want the text so have some sense. 我正在使用一些Javascript将字符串切成140个字符,而又不会打断单词和东西,但是现在我希望文本具有某种意义。 so i would like if you find a character (just like ., , :, ;, etc) and if the string is>110 characters and <140 then slice it, so the text has more sense.
所以我想如果您找到一个字符(就像。,,,:,;等),并且如果字符串是> 110个字符而<140,则将其切成薄片,这样文本就更有意义了。 Here is what i have done: where texto means text, longitud means length, and arrayDeTextos means ArrayText.
这是我所做的事情:其中texto表示文本,longitud表示长度,而arrayDeTextos表示ArrayText。 Thank you.
谢谢。
//function to cut strings
function textToCut(texto, longitud){
if(texto.length<longitud) return texto;
else {
var cortado=texto.substring(0,longitud).split(' ');
texto='';
for(key in cortado){
if(key<(cortado.length-1)){
texto+=cortado[key]+' ';
if(texto.length>110 && texto.length<140) {
alert(texto);
}
}
}
}
return texto;
}
function textToCutArray(texto, longitud){
var arrayDeTextos=[];
var i=-1;
do{
i++;
arrayDeTextos.push(textToCut(texto, longitud));
texto=texto.replace(arrayDeTextos[i],'');
}while(arrayDeTextos[i].length!=0)
arrayDeTextos.push(texto);
for(key in arrayDeTextos){
if(arrayDeTextos[key].length==0){
delete arrayDeTextos[key];
}
}
return arrayDeTextos;
}
Break the string into sentences, then check the length of the final string before appending each sentence. 将字符串分成句子,然后在附加每个句子之前检查最终字符串的长度。
var str = "Test Sentence. Test Sentence";
var arr = str.split(/[.,;:]/) //create an array of sentences delimited by .,;:
var final_str = ''
for (var s in arr) {
if (final_str.length == 0) {
final_str += arr[s];
} else if (final_str.length + s.length < 140) {
final_str += arr[s];
}
}
alert(final_str); // should have as many full sentences as possible less than 140 characters.
I think Martin Konecny's solution doesn't work well because it excludes the delimiter and so removes lots of sense from the text. 我认为Martin Konecny的解决方案效果不佳,因为它排除了定界符,从而使文本失去了很多含义。
This is my solution: 这是我的解决方案:
var arrTextChunks = text.split(/([,:\?!.;])/g),
finalText = "",
finalTextLength = 0;
for(var i = 0; i < arrTextChunks.length; i += 2) {
if(finalTextLength + arrTextChunks[i].length + 1 < 140) {
finalText += arrTextChunks[i] + arrTextChunks[i + 1];
finalTextLength += arrTextChunks[i].length;
} else if(finalTextLength > 110) {
break;
}
}
http://jsfiddle.net/Whre/3or7j50q/3/ http://jsfiddle.net/Whre/3or7j50q/3/
I'm aware of the fact that the i += 2
part does only make sense for "common" usages of punctuation (a single dot, colon etc.) and nothing like "hi!!!?!?1!1!". 我知道以下事实:
i += 2
部分仅对标点符号的“常用”用法(单个点,冒号等)有意义,而没有像“ hi !!!?!?1!1!”之类的东西。 。
Should be a bit more effective without regex splits. 没有正则表达式拆分,应该会更有效。
var truncate = function (str, maxLen, delims) {
str = str.substring(0, maxLen);
return str.substring(0, Math.max.apply(null, delims.map(function (s) {
return str.lastIndexOf(s);
})));
};
试试这个正则表达式,您可以在这里看到它的工作原理: http ://regexper.com/#%5E(%5Cr%5Cn%7C.)%7B1%2C140%7D%5Cb
str.match(/^(\r\n|.){1,140}\b/g).join('')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.