Trim a string in JavaScript

No more fat…

I’ve seen all kinds of techniques used to trim a string in JavaScript, some of them a bit bonkers. Here’s the ‘right’ way:

var trimmed = str.replace(/^\s+|\s+$/g, '') ;

So there.

Latest News & Insights

Say connected – get Loft updates straight to your inbox.