CyrefJS
CCyrefJS
فا
Back to documentation
String/Since 1.38.0

removeWhitespace

Removes all whitespace characters from a string.

removeWhitespace(value):string

Behavior

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

valuestringrequired

The string whose whitespace characters should be removed.

Returns

string

The input string with all matched whitespace characters removed.

Examples

01

Basic usage

Remove spaces from a string.

TypeScript
import { removeWhitespace } from "@cyref/js";

removeWhitespace("hello world");
// "helloworld"
02

Multiple spaces

All spaces are removed, including consecutive spaces.

TypeScript
removeWhitespace("hello   beautiful   world");
// "hellobeautifulworld"
03

Tabs and newlines

Whitespace such as tabs and newlines is removed as well.

TypeScript
removeWhitespace("hello\tworld\nagain");
// "helloworldagain"
04

Mixed whitespace

Different whitespace characters are removed wherever they occur.

TypeScript
removeWhitespace(" hello \t world \n ");
// "helloworld"
05

Whitespace-only string

A string containing only whitespace becomes empty.

TypeScript
removeWhitespace(" \t\n ");
// ""
06

Empty string

An empty string remains empty.

TypeScript
removeWhitespace("");
// ""
07

Text without whitespace

A string without whitespace is returned unchanged.

TypeScript
removeWhitespace("helloWorld");
// "helloWorld"
Built with@cyref/js·Official documentation