Remove whitespaces

OSCC · JavaScript, String, Regexp · Nov 3, 2020

Returns a string with whitespaces removed.

  • Use String.prototype.replace() with a regular expression to replace all occurrences of whitespace characters with an empty string.
const removeWhitespace = str => str.replace(/\s+/g, '');
removeWhitespace('Lorem ipsum.\n Dolor sit amet. ');
// 'Loremipsum.Dolorsitamet.'

Would you like to help us improve 30 seconds of code?Take a quick survey

Recommended snippets