Here’s a one-liner to remove all whitespace from a string in JavaScript:
const stringWithoutWhitespace = originalString.replace(/\s+/g, "");
This uses the replace()
method with a regular expression that matches one or more whitespace characters (\s+
), and replaces them with an empty string. The g
flag at the end of the regular expression makes it global, meaning it will replace all occurrences of whitespace in the string.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.