Split String after x characters and space

Hey,
I’m looking for a code that splits my text after 50 characters at the next space.

Like on this example from @code-queen

But I want to split the text after the end of the word (at the next space).

I tryied so muchen codes from the internet, but I haven’t found one working.

I hope you can help me :grinning:

Here’s a simple function that I found. You can use this in your code:

const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;
let short = truncate("This is a very, very, long message and we're going to make it shorter.", 30, '...'); 
console.log('short msg', msg);

Thanks, you’re awesome! Now it’s working.

But “msg” is undefined (but I don’t think it’s important for the function).

Here’s my code for everybody else, who has this problem:

function limitCharacters() {
    $w('#repeater2').onItemReady(($item, itemData) => {

 const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(18, str.substr(18, max - suffix.length).lastIndexOf(' '))}${suffix}`;
 let shortDesc = truncate(itemData.description, 130, '...');

        $item("#text65").text = itemData.title;
        $item("#text64").text = shortDesc;
        $item("#button3").link = itemData['link-CollectionName-title'];
    });
}

Both numbers in “truncate” have to be the same!
These both numbers determine after how many characters the first text is displayed.
The number under “shortDesc” limits the characters.