var string = "    This    should  become   something          else   too . ";
string = string.replace(/\s+/g, " ");

This code replaces a consecutive set of whitespace characters (\s+) by a single white space. Note that a white-space character also includes tab and newlines. Replace \s by a space if you only want to replace spaces.

If you also want to remove the whitespace at the beginning and end, include:

string = string.replace(/^\s+|\s+$/g, "");

This line removes all white-space characters at the beginning (^) and end ($). The g at the end of the RegExp means: global, ie match and replace all occurences.

Answer from Rob W on Stack Overflow
๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ remove-extra-spaces-from-a-string-in-javascript-or-node-js
Remove Extra Spaces From a String in JavaScript or Node.js
This tutorial shows you how to remove extra spaces from a string. Removing extra spaces results in a string value where words are separated by a single space. ... Use JavaScriptโ€™s string.replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace ...
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript remove whitespace of string | beginning & end, between
JavaScript Remove Whitespace of String | beginning-end, between
May 15, 2021 - \s is the regex for โ€œwhitespaceโ€, and g is the โ€œglobalโ€ flag, meaning match ALL \s (whitespaces). ... The trim() method will remove whitespace from the beginning and end of the string in JavaScript.
๐ŸŒ
regex101
regex101.com โ€บ library โ€บ jS1iV6
regex101: Remove multiple white spaces between words
RegEx email /^((?!\.)[\w-_.]*)(@\w+)(\.\w+(\.\w+)?)$/gim; Just playing with Reg Ex. This to validate emails in following ways The email couldn't start or finish with a dot The email shouldn't contain spaces into the string The email shouldn't contain special chars ( mailname@domain.com First group takes the first string with the name of email \$1 => (mailname) Second group takes the @ plus the domain: \$2 => (@domain) Third group takes the last part after the domain : \$3 => (.com) Submitted by https://www.linkedin.com/in/peralta-steve-atileon/
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-remove-spaces-from-a-string-using-javascript
How to Remove Spaces From a String using JavaScript? | GeeksforGeeks
Here are different ways to remove all line breakes from a string in JavaScript.1. Using JavaScript RegEx with replace() Method This method uses regular expressions to detect and replace newlines in the string.
Published ย  November 14, 2024
Find elsewhere
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ remove-all-whitespaces-from-a-text
JavaScript Program to Remove All Whitespaces From a Text | Vultr Docs
December 18, 2024 - Define your text string that includes various whitespace characters. Use the regular expression /\s+/g in combination with the replace() method to remove all forms of whitespace. ... var originalText = "Here is some text with a lot of spaces."; ...
๐ŸŒ
RegExr
regexr.com โ€บ 3hc0e
Remove digits, spaces, etc.
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp). Supports JavaScript & PHP/PCRE RegEx.
๐ŸŒ
Texthandler
texthandler.com โ€บ info โ€บ remove-double-spaces-javascript
Replace multiple spaces in a string to single space with JavaScript
If you need to replace multiple spaces (double spaces) in a string to single space with JavaScript you can use next string method: replace(regexp/substr, newstring) The replace() method searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring. Text = Text.replace(/ {1,}/g," "); Also you can use online service for remove double spaces and multiple spaces ยท
๐ŸŒ
Flavio Copes
flaviocopes.com โ€บ home โ€บ javascript โ€บ how to replace white space inside a string in javascript
How to replace white space inside a string in JavaScript
December 3, 2019 - Learn how to remove or replace all the whitespace inside a string in JavaScript with a regular expression, using the whitespace metacharacter and the g flag.
๐ŸŒ
EncodedNA
encodedna.com โ€บ javascript โ€บ remove-whitespace-from-string-using-regex-in-javascript.htm
How to Remove all whitespace from a string using RegEx in JavaScript
&lthtml> &ltbody> &ltinput type="text" ... function removeSpaces() { var oldCode = document.getElementById('oldCode'); var str = oldCode.value; // STORE TEXTBOX VALUE IN A STRING. var newCode = document.getElementById('newCode'); newCode.innerHTML = str.replace(/ /g, "") + ' &lti>(all whitespaces removed)&lt/i>'; } &lt/script> &lt/html> ... I am using JavaScript replace() method here in the script. The method takes "two" parameters. In the first parameter, I have two forward slash (separated by a space), followed by the RegExp g modifier...
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ javascript-and-regex-remove-space-after-the-last-word-in-a-string
How to Remove Trailing Space After Last Word in a JavaScript String Using Regex โ€” javaspring.net
Control: Specify which whitespace characters to remove (e.g., spaces but not tabs). Flexibility: Handle edge cases (e.g., mixed whitespace like spaces + tabs). Regex is a lightweight, built-in tool in JavaScript, making it ideal for this task.
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ javascript-remove-all-whitespace-from-string-ece685d0ec33
How to Remove All Whitespace from a String in JavaScript | JavaScript in Plain English
July 22, 2024 - To remove all whitespace from a string in JavaScript, call the replace() method on the string, passing a regular expression that matches any whitespace character, and an empty string as a replacement.
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Remove Spaces From String | W3Docs
If you want to remove all the space in the string, you can use the replace() method: ... Output appears here after Run. Here \s is the regex for whitespace, and g is the global flag, which matches all occurrences.
๐ŸŒ
Medium
medium.com โ€บ @tariibaba โ€บ javascript-remove-all-spaces-from-string-75f942ef2fbe
How to Remove All Spaces from a String in JavaScript | Medium
June 22, 2022 - Alternatively, we can remove all spaces from a string by calling the replace() method on the string, passing a regular expression matching any space as the first argument, and an empty string ('') as the second. const str = 'A B C'; const ...