javascript - 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 -
the code looks this:
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; }
the problem have following thing:
hello world , an... curse of gaming backlog –...
i want make sure instead does:
hello world and... curse of gaming backlog...
i guess need make sure there alphabet char (a,b,c,d etc..) , no special characters.
any kind of appreciated
you might want start this:
function cutoff(str, maxlen) { // no need cut off if(str.length <= maxlen) { return str; } // find 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 ellipsis return str.substring(0,pos) + "..."; }
which @ least gives cutoffs based on words, rather letters. if need additional filtering, can add it, give cutoff such "the curse of gaming backlog – ..." doesn't wrong, honestly.
Comments
Post a Comment