[英]Remove all but the three word from a sentence and add three dots after that three sentence using jquery
我想从句子中删除除三个单词以外的所有单词,并使用jquery在该三个句子之后添加三个点。 例如,如果句子为“我们定期评估我们的借款人。请参阅我们的评估”,以便定期删除“ 借款人。请参阅我们的评估请参阅我们的评估 ”,我想这样表示:“ 我们评估我们的。 “我想这里是代码
您可以使用CSS规则text-overflow
来实现此目的。 这是您可以用来实现所需结果的类:
.prevent-overflow {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis
}
下面是它的功能细分: white-space: nowrap;
当到达元素的边缘时,停止将文本表单换行。 overflow:hidden
告诉文本永远不会出现在元素和text-overflow: ellipsis
的范围之外text-overflow: ellipsis
告诉浏览器在任何内容被截断时将...
放置在文本的末尾(请参阅text-overflow @ MDN )。
将其应用于尺寸会截断其中包含的文本的元素。
我创建了一个jsfiddle来演示其用法。
注意: jsfiddle在元素( <div style="clear:both"></div>
)的末尾包含一个div,这会阻止省略号的工作(请参阅此jsfiddle ),我建议您应用clear:both;
而不是整个div,或者找到另一种方法来实现<div>
的目的。
Javascript仅支持先行但不支持落后...但是的,存在解决方法
这将为您服务...
var str="How are you doing today?";
regexp = new RegExp('(' + str.match(/(\w+\s){2}\w+/g)+ ')?.*'); //fetches first 3 words and makes regural expression to lookbehind this positively
var output = str.replace(regexp, function($0, $1){ //mimicks positive lookbehind
return $1 ? $1 + '...' : $0;
});
document.writeln(output );
更新-这仅适用于“你好吗..” ...所以请使用以下内容
这是一个很好的教程,用于模拟javascript中的负向和正向隐藏,因为它不支持它。
Javascript仅支持先行
http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
更新-这对于您的示例中的任何东西都适用
var str="We evaluate our borrowers on a periodic basis. See our evaluationsWe evaluate our borrowers on a periodic basis. See our evaluationsWe evaluate our borrowers on a periodic basis. See our evaluations";
var first_three = str.match(/(\w+\s){2}\w+/);
regexp = new RegExp('(' + first_three[0] + ')?.*'); //fetches first 3 words and makes regural expression to lookbehind this positively
var output = str.replace(regexp, function($0, $1){ //mimicks positive lookbehind
return $1 ? $1 + '...' : $0;
});
document.writeln(output);
和正在运行的jsFiddle http://jsfiddle.net/Rpq7b/2/
尝试以下功能:
function myfunction(val)
{
temp = val.split(' ');
if(val.length >= 2)
{
val = temp[0] + ' ' + temp[1] + ' ' + temp[2] + ' ..';
}
return val;
}
我想这应该工作
var yourText = " We evaluate our borrowers on a periodic basis",
result = yourText.replace(/^(.{15}[^\s]*).*/, "$1");
alert(result);
注意:将代码中的15
乘以任意数量
要获得省略号,您可以添加result
+ "…"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.