• 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:

  • Two ways to remove a specific item in an array
  • Create a new array from an existing one
  • Show a loading indicator when an iframe is being loaded
  • Sorting Objects with a Primary and Secondary Property

Primary Sidebar

JSON Compare

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2026 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers
Advertisement