Files
30-seconds-of-code/snippets/removeAllWhitespaces.md
Rahul Dahal 2d919d88c6 Create removeAllWhitespaces.md
Uses `String.prototype.replace()` with a regular expression to replace any and all occurrences of whitespace characters with a empty string.
2020-10-13 11:59:46 +05:45

615 B

title, tags
title tags
removeAllWhitespaces string,regexp,beginner

Returns a string removing any and all whitespaces.

  • Use String.prototype.replace() with a regular expression to replace any and all occurrences of whitespace characters with a empty string.
const removeAllWhitespaces = (string) => {
  if(!string || typeof string !== "string") return "";
  return string.replace(/\s+/g, ""); // trimming the whitespace, if any
}

removeAllWhitespaces(" Hello, I've a lot of white spaces. \n Including a line break."); // "Hello,I'vealotofwhitespaces.Includingalinebreak."