Files
30-seconds-of-code/snippets/compact-whitespace.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

523 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Compact whitespaces string,regexp travel-mug-1 2018-12-12T19:11:33+02:00 2020-10-18T23:04:45+03:00

Compacts whitespaces in a string.

  • Use String.prototype.replace() with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space.
const compactWhitespace = str => str.replace(/\s{2,}/g, ' ');
compactWhitespace('Lorem    Ipsum'); // 'Lorem Ipsum'
compactWhitespace('Lorem \n Ipsum'); // 'Lorem Ipsum'