434 B
434 B
title, tags
| title | tags |
|---|---|
| compactWhitespace | string,regexp,beginner |
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'