759510
string.js中有一段源码,截取字符串的,中文有点问题,看了源码,有一段不是很明白,求教:
var tmpl = function(c){ return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' '; },
template = str.slice(0, length+1).replace(/.(?=\W*\w*$)/g, tmpl); // 'Hello, world' -> 'HellAA AAAAA'
ps : str就是普通字符串 这段的目的是什么?
应小伙伴要求贴出方法全部:
truncate: function(length, pruneStr) { //from underscore.string, author: github.com/rwz
var str = this.s;
length = ~~length;
pruneStr = pruneStr || '...';
if (str.length <= length) return new this.constructor(str);
var tmpl = function(c){ return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' '; },
template = str.slice(0, length+1).replace(/.(?=\W*\w*$)/g, tmpl); // 'Hello, world' -> 'HellAA AAAAA'
if (template.slice(template.length-2).match(/\w\w/))
template = template.replace(/\s*\S+$/, '');
else
template = new S(template.slice(0, template.length-1)).trimRight().s;
return (template+pruneStr).length > str.length ? new S(str) : new S(str.slice(0, template.length)+pruneStr);
}