Given that you also want to cover tabs, newlines, etc, just replace \s\s+ with ' ':

string = string.replace(/\s\s+/g, ' ');

If you really want to cover only spaces (and thus not tabs, newlines, etc), do so:

string = string.replace(/  +/g, ' ');
Answer from BalusC on Stack Overflow
🌐
Reddit
reddit.com › r/dataengineering › replacing multiple spaces with a single space
r/dataengineering on Reddit: Replacing multiple spaces with a single space
September 25, 2024 -

At first I thought this task won’t take more than half hour for me as the code was converting the dataframe into a temporary view and using sql on it. But the problem arose when I started testing and the escaping character was also eliminated then the special spaces like \n, \t started eliminating.

My task is to replicate just was oracle replaces that is just the multiple spaces with single space excluding all the escape characters and or the special spaces.

Can someone please help me on this. It will work if you suggest any sql regex replace or even any pyspark, pandas code to get it done as I am performing it in Databricks.

Thanks in advance🤗

Discussions

removed random amount of extra spaces from string
Hello i am getting a string input that sperates a data table with a random amount of spaces. Is there a way to replace All space sequences with a single space ? e.g. CPU MEM PID 15.689 12.2 6610 21.8 156 6611 (so the spaces are random) I have ried replaces double spa... More on forums.ni.com
🌐 forums.ni.com
1
0
June 27, 2015
java - Regex or way to replace multiple space with single space - Stack Overflow
Could you please let me know is there any way to replace multiple space with single space in java or spring? Any stringUtils function for same? like 1. test test test test 2. test test test More on stackoverflow.com
🌐 stackoverflow.com
September 17, 2013
RegEx how to replace multiple spaces with a single one
Possible Duplicate: Remove all multiple spaces in Javascript and replace with single space How can I replace, for instance this: " " " " " " " " with " " How can I replace all the e... More on stackoverflow.com
🌐 stackoverflow.com
How can I use regex on multiple spaces?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
12
3
January 4, 2025
🌐
Attacomsian
attacomsian.com › blog › javascript-replace-multiple-spaces-with-single-space
How to replace multiple spaces with a single space in JavaScript
October 23, 2022 - To replace multiple spaces with a single space in JavaScript, use the replace() method with a regex that matches all two or more consecutive whitespace characters.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-replace-multiple-spaces-in-a-string-using-a-single-space-using-java-regex
How to replace multiple spaces in a string using a single space using Java regex?
November 21, 2019 - import java.util.Scanner; public class Test { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression to match space(s) String regex = "\s+"; //Replacing the pattern with single space String result = input.replaceAll(regex, " "); System.out.print("Text after removing unwanted spaces: \n"+result); } }
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-replace-multiple-spaces-with-single-space-in-javascript
JavaScript - How to Replace Multiple Spaces with a Single Space? - GeeksforGeeks
July 23, 2025 - The replace() method combined with a regular expression is the most efficient way to replace multiple spaces with a single space.
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 16653 › need-help-on-replacing-multiple-spaces-to-a-single-space-using-regular-expression
Need help on replacing multiple spaces to a single space using Regular Expression | Notepad++ Community
November 18, 2018 - ... I’m sure there is a certain ... idea? Thanks in advance and appreciate for your suggessions. ... Put a single space followed by a + in the Find what box....
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-replace-multiple-spaces-with-a-single-space-in-chash
How to replace multiple spaces with a single space in C#?
March 17, 2026 - using System; using System.Text.RegularExpressions; namespace DemoApplication { class Program { public static void Main() { string stringWithMultipleSpaces = "Hello World. Hi Everyone"; Console.WriteLine($"String with multiple spaces: {stringWithMultipleSpaces}"); string stringWithSingleSpace = Regex.Replace(stringWithMultipleSpaces, @"\s+", " "); Console.WriteLine($"String with single space: {stringWithSingleSpace}"); } } }
Find elsewhere
🌐
IncludeHelp
includehelp.com › code-snippets › regex-to-replace-multiple-spaces-with-a-single-space-in-javascript.aspx
Regex to replace multiple spaces with a single space in JavaScript
September 7, 2022 - The newValue will be a singular space specified like '' . If 'g' is not specified in the regular expression, then this will just replace the first expression matched. Therefore to replace all the matched expressions we need to specify the 'g' at the end of the regex.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-replace-multiple-spaces-with-single-space
Replace multiple spaces with a single space in Python | bobbyhadz
April 9, 2024 - The re.sub method will return a new string that is obtained by replacing all occurrences of multiple spaces with a single space.
🌐
CodingTechRoom
codingtechroom.com › question › -replace-multiple-spaces-with-single-space
How to Replace Multiple Spaces with a Single Space Using Regex? - CodingTechRoom
In PHP, the preg_replace function can be used for this purpose: preg_replace('/\s+/', ' ', $string); Mistake: Not including regex flags which may cause issues in certain strings.
🌐
Programming Idioms
programming-idioms.org › idiom › 219 › replace-multiple-spaces-with-single-space › 3802 › python
Replace multiple spaces with single space, in Python
var t = s.replaceAll(RegExp(r"\s+"), " "); Demo · Doc · singleSpace(Text) -> singleSpace(0, Text). singleSpace(_, []) -> []; singleSpace(32, [32 | Rest]) -> singleSpace(32, Rest); singleSpace(32, [Ch | Rest]) -> [Ch] ++ singleSpace(Ch, Rest); singleSpace(Last, [Ch | Rest]) -> [Ch] ++ ...
🌐
O'Reilly
oreilly.com › library › view › regular-expressions-cookbook › 9781449327453 › ch05s13.html
5.13. Replace Repeated Whitespace with a Single Space - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - Any tabs, line breaks, or other whitespace should also be replaced with a space. To implement either of the following regular expressions, simply replace all matches with a single space character.
Authors   Jan GoyvaertsSteven Levithan
Published   2012
Pages   609
🌐
Alvin Alexander
alvinalexander.com › scala › how-to-convert-multiple-spaces-to-single-space-regex
Scala: How to convert multiple spaces to a single space (regex) | alvinalexander.com
As that example shows, the key is to first call trim to remove any leading and trailing spaces, and then call replaceAll to convert one or more blank spaces to a single space: val after = before.trim.replaceAll(" +", " ") (The code I show is a simple adaptation of the Java code I found on SO.) ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-replace-multiple-spaces-with-single-space-in-javascript
JavaScript – How to Replace Multiple Spaces with a Single Space? | GeeksforGeeks
November 26, 2024 - The replace() method combined with a regular expression is the most efficient way to replace multiple spaces with a single space.