2016-12-29 169 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 + '...'; 
 
}