1.38.0removeWhitespace
Removes all whitespace characters from a string.
removeWhitespace(value):stringBehavior
removeWhitespace removes every whitespace character matched by the regular expression from the input string. No replacement character is inserted.
Whitespace handling
The regular expression uses the Unicode-aware whitespace character class, so whitespace recognized by JavaScript's Unicode regular expression behavior is removed.
All occurrences
The global flag causes every matching whitespace character in the string to be removed, not only the first occurrence.
No replacement
Unlike normalizeWhitespace, this utility does not replace whitespace with a space. It removes matched whitespace characters completely.
Parameters
valuestringrequiredThe string whose whitespace characters should be removed.
Returns
stringThe input string with all matched whitespace characters removed.
Examples
Basic usage
Remove spaces from a string.
import { removeWhitespace } from "@cyref/js";
removeWhitespace("hello world");
// "helloworld"Multiple spaces
All spaces are removed, including consecutive spaces.
removeWhitespace("hello beautiful world");
// "hellobeautifulworld"Tabs and newlines
Whitespace such as tabs and newlines is removed as well.
removeWhitespace("hello\tworld\nagain");
// "helloworldagain"Mixed whitespace
Different whitespace characters are removed wherever they occur.
removeWhitespace(" hello \t world \n ");
// "helloworld"Whitespace-only string
A string containing only whitespace becomes empty.
removeWhitespace(" \t\n ");
// ""Empty string
An empty string remains empty.
removeWhitespace("");
// ""Text without whitespace
A string without whitespace is returned unchanged.
removeWhitespace("helloWorld");
// "helloWorld"