当超过140个字符时,如何减少组合的推文?

时间:2016-04-22 18:16:53

标签: javascript node.js twitter

我有一个tweet函数可以删除@ -mentions,并且在给定以下条件的情况下不会发推文: 1)如果问题与答案不同, 2)如果组成的推文大于140个字符和 3)如果推文可能敏感。

工作正常,但如果长度超过140,我宁愿削减组合answer。理想情况下,我想要除了前137个字符之外的所有字符并添加" ..."我不确定最好的方法是什么。

如何削减这条推文?

以下是当前代码:

function(tweet) {

var question = tweet.txt;
var answer = tweet.txt + "some text";

if(question !== answer && answer.length < 140 && !tweet.possibly_sensitive) {
       answer = answer.replace(/@/g, "."); //removes @-mentions.
       return { id_str: tweet.id_str, text: answer };
     }
}

1 个答案:

答案 0 :(得分:0)

您可以使用JavaScript中的String对象提供的slice()来删除答案字符串的结尾。

// returns a new answer string containing the first 137 characters
// with '...' tacked onto the end
answer = answer.slice(0,137) + '...'; `