2016-12-29 170 views
4

免责声明:我是编程新手!我看过S.O.在发布之前找到这个问题的答案,但没有找到我需要的答案。Javascript:如何从字符串末尾去除标点符号

现在,我正在使用的API返回一个变量:'description'。 'description'是一个动态的,带有标点符号的250个字符的字符串。

我必须截断字符串为110个字符,然后在它后面插入一个省略号。这是很容易做到 - 我一直在使用类似:

description.slice(0,110) + "..." 

但上面是有问题的,因为我无法预知我的字符串将截断什么性格的方式。如果截断标点符号或空格,结果看起来真的很傻:

enter image description here

我已经读了很多类似的咨询,开发人员想知道如何起飞一个标点字符在最后的一个字符串。但是我可能不得不取消几个标点符号,这取决于变量返回多少标点或空格。

任何人都可以告诉我有关这方面的最佳方法吗?如果我可以提供任何其他信息,请让我知道。

+1

这里有一个唯一的CSS-替代(虽然我不认为它允许你控制截断发生在哪一个字符):http://caniuse.com/#feat=text-overflow – jtbandes

+0

检查切片描述,使用charAt(str.length-1),添加条件...如果点在最后,添加两个点,如果不是 - > 3 ...类似的东西.... – sinisake

+1

我会推荐一种不同的方法:根据单词进行分裂,并粘合不超过11​​0个字符的单词。通过这种方式,你将确保你只会得到整个单词,而不是像笨蛋那样愚蠢的事情...... ** –

回答

1

我认为这会工作!

function truncateWholeWords (text, maxLength) { 
    maxLength = maxLength || 110; 
    var length = 0; 

    return text.split(' ').filter(function (word) { 
     length += (word.length+1); 
     return length <= maxLength; 
    }).join(' ').replace(/([.,\/#!$%\^&\*;:{}=\-_`~()\]\[])+$/g, "") + '...'; 

} 
1

你可以使用装饰在最后去除多余的空格:

var description = description.slice(0,110).trim(); //trim to remove spaces 

然后添加一个条件,以检查是否有在最后一个点,如果是添加了两个人还添加三个标点符号:

if (description[description.length-1] === ".") 
    description += '..'; 
else 
    description += '...'; 

希望这会有所帮助。

var description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; 
 

 
description = description.slice(0,110).trim(); 
 

 
if (description[description.length-1] === ".") 
 
    description += '..'; 
 
else 
 
    description += '...'; 
 

 
console.log(description);

片段与.和空格结尾:

var description = "Lorem ipsum dolor sit amet,consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore. magna aliqua."; 
 

 
description = description.slice(0,110).trim(); 
 

 
if (description[description.length-1] === ".") 
 
    description += '..'; 
 
else 
 
    description += '...'; 
 

 
console.log(description);

3

每我的意见,我想接近它略有不同,以保证整个词。

var string = "This is a sentence. A long sentence that should be broken up so it's not too long. Got it? Good. How long. does it get?"; 
 

 
var excerpt = createExcerpt(string); 
 
console.log(excerpt); 
 

 
// Function to parse a sentence into an excerpt, based on whole words 
 
function createExcerpt(string, maxLength) { 
 
    // Set a default value of maxLength of 110 
 
    maxLength = maxLength | 110; 
 
    // If it's not too long, don't do anything 
 
    if (string.length <= maxLength) { 
 
    return string; 
 
    } 
 
    
 
    // Break it up into words 
 
    var words = string.split(' '); 
 
    var excerpt = ''; 
 
    // Loop over the words in order 
 
    words.forEach(function(word) { 
 
    // Build a test string to see if it's too long 
 
    test = excerpt + ' ' + word; 
 
    // If it's too long, then break out of the loop 
 
    if (test.length > maxLength) { 
 
     return false; 
 
    } 
 

 
    // Otherwise, set the excerpt to the new test string 
 
    excerpt = test; 
 
    }); 
 

 
    // Remove any extra spaces/dots at the end of the excerpt 
 
    excerpt = excerpt.replace(/[\s|\.]+$/i, ''); 
 
    // Return the excerpt with ellipses 
 
    return excerpt + '...'; 
 
}