Javascript: how to remove punctuation from the end of a line

Disclaimer: I am new to programming! I looked at S.O. to find the answer to this question before posting, but did not find the answer I need.

Currently, the API I'm working with returns a variable: 'description'. "description" is a dynamic 250-character string with punctuation.

I need to truncate a string to 110 characters and then insert an ellipsis into it. It's simple enough - I used something like:

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

But the above is problematic, because I have no way to predict which character my string will truncate to. If it truncates punctuation or a space, the result looks really stupid:

enter image description here

, , . , , , , .

- ? , , .

+4
3

, !

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 string = "This is a sentence. A long sentence that should be broken up so it 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 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 too long
    test = excerpt + ' ' + word;
    // If it 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 + '...';
}
Hide result
+3

, :

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);
Hide result

. :

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);
Hide result
+1
source

Source: https://habr.com/ru/post/1665273/


All Articles