snippetjavascriptTip
Check, compact or remove whitespaces in a JavaScript string
Viewed 0 times
javascriptcheckcompactremovewhitespacesstring
Problem
Whitespace refers to characters which are used to provide horizontal or vertical space between other characters. In regular expressions,
You can use
To remove whitespaces from a string, you can simply use
Similar to the previous example, you can use
\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'); // trueTo 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'); // trueconst 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.