• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Remove whitespaces from a string

Remove whitespaces from a string

Remove whitespaces from a string

Returns a string with whitespaces removed. Use String.prototype.replace() with a regular expression to replace all occurrences of whitespace characters with an empty string.

const removeWhitespace = str => str.replace(/\s+/g, '');

// Example
removeWhitespace('Lorem ipsum.\n Dolor sit amet. ');
// 'Loremipsum.Dolorsitamet.'

Source

https://www.30secondsofcode.org/js/s/remove-whitespace

Miscellaneous

Related Snippets:

  • Transform the items in an array and create a new one
  • Xmas Tree Generator
  • Add an element to the DOM
  • Take the content of an array and return a single value

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers