我知道这个问题类似于THIS,所以如果认为是重复的话就道歉。
我想要达到的结果是使用类 gallery-info
检查divint i;
gsl_matrix * W = gsl_matrix_alloc(10,10);
for(i=1; i < 101; i++){
gsl_matrix_free(W);
W = gsl_matrix_alloc(10,10);
}
如果每个div的字符数大于140个字符,我设置了一个返回子字符串的条件。特(Twitter)
$('.gallery-info').each(function() {
});
如果
子字符串的最后一个字符与 patt var匹配。 返回子串-1并连接“...”
ELSE
返回子串并连接“......”
我在这方面已经有了大脑放屁,我相信我可以使用str.replace()和str.charAt()在Vanilla JS中实现这一点,但我需要在jQuery中执行此操作。
答案 0 :(得分:1)
我认为这就像你所描述的那样有效。
$('.gallery-info').each(function() {
var descLength = 140;
var str = $(this).text();
var patt = new RegExp(/[,;.:!()&\s$]/g);
if (str.length > descLength) {
var substring = str.substr(0, descLength);
var lastChar = substring[substring.length-1];
if (lastChar.match(patt)) {
$(this).text(str.substr(0, descLength -1) + "...");
} else {
$(this).text(str.substr(0, descLength) + "...");
}
}
});
<强> Codepen 强>
答案 1 :(得分:1)
我认为用下面的方法更新您的IF条件应该可以正常工作。
if (str.length > descLength) {
if(patt.test(str[descLength-1])) {
$(this).text(str.substr(0, descLength-1) + "...");
} else {
$(this).text(str.substr(0, descLength) + "...");
}
}
CODEPEN :https://codepen.io/azimjs/pen/mBqjNY