HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Check, compact or remove whitespaces in a JavaScript string

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptcheckcompactremovewhitespacesstring

Problem

Whitespace refers to characters which are used to provide horizontal or vertical space between other characters. In regular expressions, \s is used to match any whitespace character. Using this knowledge, we can create a variety of useful functions to work with whitespace in JavaScript strings.
You can use RegExp.prototype.test() with a simple regular expression (/\s/) to check if at least one whitespace character is present in the given string.
To remove whitespaces from a string, you can simply use String.prototype.replace(). For the regular expression to match all whitespace characters, you can use the global flag (g). You should also use the + quantifier to match one or more whitespace characters for replacement.
Similar to the previous example, you can use String.prototype.replace() with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space. You can use the {2,} quantifier to match 2 or more whitespace characters and, again, the global flag (g) to match all occurrences.

Solution

const containsWhitespace = str => /\s/.test(str);

containsWhitespace('lorem'); // false
containsWhitespace('lorem ipsum'); // true


To remove whitespaces from a string, you can simply use String.prototype.replace(). For the regular expression to match all whitespace characters, you can use the global flag (g). You should also use the + quantifier to match one or more whitespace characters for replacement.
Similar to the previous example, you can use String.prototype.replace() with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space. You can use the {2,} quantifier to match 2 or more whitespace characters and, again, the global flag (g) to match all occurrences.

Code Snippets

const containsWhitespace = str => /\s/.test(str);

containsWhitespace('lorem'); // false
containsWhitespace('lorem ipsum'); // true
const removeWhitespace = str => str.replace(/\s+/g, '');

removeWhitespace('Lorem ipsum.\n Dolor sit amet. ');
// 'Loremipsum.Dolorsitamet.'
const compactWhitespace = str => str.replace(/\s{2,}/g, ' ');

compactWhitespace('Lorem    Ipsum'); // 'Lorem Ipsum'
compactWhitespace('Lorem \n Ipsum'); // 'Lorem Ipsum'

Context

From 30-seconds-of-code: find-remove-compact-whitespace

Revisions (0)

No revisions yet.