繁体   English   中英

我如何确保当我的省略号被添加为lastindexof空间时,它也在添加之前检查其字母,而不是特殊字符

[英]How can I make sure that when my ellipsis gets added lastindexof space it also checks its alphabet before it gets added and not special chars

代码如下:

 function TrimLength(text, maxLength) {
        text = $.trim(text);

        if (text.length > maxLength) {
            text = text.substring(0, maxLength - ellipsis.length)
            return text.substring(0, text.lastIndexOf(" ")) + ellipsis;
        }
        else
            return text;
    }

我的问题是它执行以下操作:

hello world and an...

The curse of the gaming backlog –...

我想确保它可以:

hello world and...

The curse of the gaming backlog...

我猜我需要确保有字母字符(例如,a,b,c,d等)并且没有特殊字符。

任何帮助都值得赞赏

您可能要从以下开始:

function cutoff(str, maxLen) {
    // no need to cut off
    if(str.length <= maxLen) {
        return str;
    }

    // find the cutoff point
    var oldPos = pos = 0;
    while(pos!==-1 && pos <= maxLen) {
        oldPos = pos;
        pos = str.indexOf(" ",pos) + 1;
    }
    if (pos>maxLen) { pos = oldPos; }
    // return cut off string with ellipsis
    return str.substring(0,pos) + "...";
}

至少给您基于单词而非字母的界限。 如果您需要其他过滤,则可以添加它,但这将为您提供诸如“游戏积压的诅咒– ...”之类的临界值,说实话,这并没有错。

暂无
暂无

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

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