When the regex matches the first time (on "A B"), this part of the string in consumed by the engine, so it is not matched again, even though your regex has the global ('g') flag.

You could achieve the expected result by using a positive lookahead ((?=PATTERN)) instead, that won't consume the match:

value = "Hello I B M"
value = value.replace(/([A-Z])\s(?=[A-Z])/g, '$1')
console.log(value) // Prints "Hello IBM"

To make it not remove the space if the next uppercase letter is the first in a word, you can increment the lookahead pattern with using a word boundary \b to make that restriction:

value = "Hello I B M Dude"
value = value.replace(/([A-Z])\s(?=[A-Z]\b)/g, '$1')
console.log(value) // Prints "Hello IBM Dude"

Note: As @CasimirHyppolite noted, the following letter has to be made optional, or the second regex won't work if the last character of the string is uppercase. Thus, the pattern ([^A-Za-z]|$), which can be read as "not a letter, or the end of the string".

Edit: Simplify lookahead from (?=A-Z) to (?=[A-Z]\b) as suggested by @hwnd

Answer from Renato Zannon on Stack Overflow
🌐
Sublime Forum
forum.sublimetext.com β€Ί t β€Ί regex-how-to-remove-empty-spaces-between-words-that-have-splited-and-to-let-one-single-space-between-words β€Ί 20586
Regex: How to Remove Empty Spaces between words that have splited and to let one single space between words? - Technical Support - Sublime Forum
May 31, 2016 - hello. I made a mistake, now all the characters from words have been split by a single space, and the words have been split by another space (that means 2 spaces). M y M o t h e r i s v e r y b e a u t i f u l. (…
🌐
Ablebits
ablebits.com β€Ί ablebits blog β€Ί excel β€Ί regex β€Ί remove whitespaces and empty lines in excel using regex
Remove whitespaces and empty lines in Excel using Regex
March 10, 2023 - To remove extra whitespace (i.e. more than one consecutive spaces), use the same regex \s+ but replace the found matches with a single space character. ... Please pay attention that this formula keeps one space character not only between words ...
Discussions

What is a regular expression for removing spaces between uppercase letters, but keeps spaces between words?
Is there a way to remove the space between 2 uppercase words and/or letters so that Face Book becomes FaceBook? ... Save this answer. Show activity on this post. When the regex matches the first time (on "A B"), this part of the string in consumed by the engine, so it is not matched again, ... More on stackoverflow.com
🌐 stackoverflow.com
Strip all white spaces between words
For more information, see the wiki Searching the Forum for Answers section More on forum.keyboardmaestro.com
🌐 forum.keyboardmaestro.com
0
0
October 30, 2019
Remove spaces from string if consecutive one letter characters or numbers - Statalist
Hi how would I go about removing spaces from strings such as the following: 1 2 B L GROW A I M INC becomes 12 BL GROW AIM INC More on statalist.org
🌐 statalist.org
February 13, 2019
Regular Expressions - Remove Whitespace from Start and End | Simpler solution? - freeCodeCamp Community - The freeCodeCamp Forum
Hello! I’ve found solution for Regular Expressions - Remove Whitespace from Start and End Challenge which I think is a little simpler than what is present as Solution in Guide Page. I’ve checked it with multiple words, devided by multiple whitespaces and I think it works as it should. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
September 28, 2022
🌐
regex101
regex101.com β€Ί library β€Ί jS1iV6
regex101: Remove multiple white spaces between words
gmOpen regex in editor Β· Remove multiple white spaces between words and replace it with the char of your choosing.Submitted by binary_fm
🌐
Netjstech
netjstech.com β€Ί 2019 β€Ί 08 β€Ί removing-spaces-between-words-in-string-Java-program.html
Removing Spaces Between Words in a String Java Program | Tech Tutorials
public class StringSpaceRemoval ... any leading and trailing spaces and normalize the spaces in between the words for that you can use trim() method along with replaceAll()....
🌐
Safe Community
community.safe.com β€Ί home β€Ί forums β€Ί fme form β€Ί general β€Ί regex: how to remove spaces pattern in a w o r d
Regex: How to remove spaces pattern in a w o r d | Community
March 13, 2021 - I think there could be a good solution to do this in two steps: First find the spaces we want, with this regexp: ... and replace those with some other character. Perhaps we need one the other way around too - that looks for a space \s after more than one letter \w{2,}. So let's say you have three StringReplacers: One to replace good trailing spaces with #, one to replace good leading spaces with #, then one "big one" to remove the incorrect spaces.
🌐
Regular-Expressions.info
regular-expressions.info β€Ί examples.html
Regular Expression Examples
This solution will also not match tags nested in themselves. You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs).
🌐
RegExr
regexr.com β€Ί 3hc0e
Remove digits, spaces, etc.
Full RegEx Reference with help & examples.
Find elsewhere
Top answer
1 of 3
2

This removes 2 or more spaces only inside <p class="text_obisnuit"> and </p> and keep any other multiple spaces.

  • Ctrl+H
  • Find what: (?:<p class="text_obisnuit">|\G)(?:(?!</p>).)*?\s\K\s+
  • Replace with: LEAVE EMPTY
  • check Wrap around
  • check Regular expression
  • DO NOT CHECK . matches newline depending if you want to match multiple lines or not.
  • Replace all

Explanation:

(?:                         # start non capture group
  <p class="text_obisnuit"> # literally
 |                          # OR
  \G                        # restart from position of last match
)                           # end group
(?:                         # start non capture group
  (?!</p>)                  # negative lookahead, make sure we haven't reach </p>
  .                         # any character
)*?                         # group may appear 0 or more times, not greedy
\s                          # a space
\K                          # forget all we have seen until this position
\s+                         # 1 or more spaces

Given text:

other     text

<p class="text_obisnuit">  The context of articles,   stories, and conversations helps you     figure out and understand the meaning   of English words in the text that are new to you.   </p>

other    text

Result for given example:

other     text

<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>

other    text

Note: it keeps space just after <p...> and just before </p>


If you want to remove these spaces, you have to run another regex:

  • Ctrl+H
  • Find what: (?<=<p class="text_obisnuit">)\s+|\s+(?=</p>)
  • Replace with: LEAVE EMPTY
  • UNcheck Match case
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

(?<=                        # start positive lookbehind, make sure we have 
  <p class="text_obisnuit"> # literally
)                           # end lookbehind
\s+                         # 1 or more spaces
|                           # OR
\s+                         # 1 or more spaces
(?=                         # start positive lookahead
  </p>                      # literally
)                           # end lookahead

Result for given example:

other     text

<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>

other    text
2 of 3
0

HTML does not in general care for blanks. If you display your HTML you will see that the blanks have disappeared.

I have created for you a JSFiddle for testing.

A much simpler solution is to just replace two blanks by one and repeat as many times as possible, but blanks are really unimportant unless in preformatted text which is using the <pre> Tag.

🌐
DigitalOcean
digitalocean.com β€Ί community β€Ί tutorials β€Ί python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
July 11, 2025 - Remove all spaces using regex: HelloWorldFromDigitalOceanHiThere Remove leading spaces using regex: Hello World From DigitalOcean Hi There Remove trailing spaces using regex: Hello World From DigitalOcean Hi There Remove leading and trailing spaces using regex: Hello World From DigitalOcean Hi There
🌐
YouTube
youtube.com β€Ί watch
How To Remove Space Between Words Using Notepad++ - YouTube
This video tutorial will show you how to remove space between words using Notepad++. We will use a regular expression (Regex) to remove all spaces from the g...
Published Β  May 17, 2021
🌐
Super User
superuser.com β€Ί questions β€Ί 1738182 β€Ί how-do-i-remove-spaces-after-a-specific-character-or-seperator-in-notepad
How do i remove spaces after a specific character or seperator in notepad++? - Super User
August 21, 2022 - A simple solution that does not need advanced Regex knowledge is just to look for :[word][space] and replace it by `:[word]", this removing all spaces that follow words after the colon.
🌐
How to do in Java
howtodoinjava.com β€Ί home β€Ί string β€Ί java – normalize extra white spaces in a string
Java - Normalize Extra White Spaces in a String - HowToDoInJava
January 6, 2023 - Learn to remove extra white spaces between words in a String in Java. Do not forget to check if the string has leading or trailing spaces.
🌐
Statalist
statalist.org β€Ί forums β€Ί forum β€Ί general-stata-discussion β€Ί general β€Ί 1483591-remove-spaces-from-string-if-consecutive-one-letter-characters-or-numbers
Remove spaces from string if consecutive one letter characters or numbers - Statalist
February 13, 2019 - version 14 clear input str30(stringvar) "1 2 B L GROW A I M INC" "W H O CREATED T H I S MESS" "R E G E X ROCK" end replace stringvar=ustrregexra(stringvar,"(?<![A-Z0-9][A-Z0-9])(?=[ ][A-Z0-9]( |$)) ","",0) list As an explanation: I understood the question as "remove any space character that (1) is followed by only single characters (and, maybe, consecutive white space) or END OF LINE, and (2) is not preceded by more than a single character". Does this do the trick? Kind regards Bela Β· Last edited by Daniel Bela; 14 Feb 2019, 08:56. Reason: formatting ... Daniel Bela - Many thanks. Your elegant code deserves my study; my knowledge of contemporary regular expression syntax is woefully incomplete. I slightly disagree; the Unicode regex engine Stata uses since version 14 seems quite comprehensive to me, and IMHO is a tremendous improvement to the former regex functions.
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί freecodecamp community
Regular Expressions - Remove Whitespace from Start and End | Simpler solution? - freeCodeCamp Community - The freeCodeCamp Forum
September 28, 2022 - Hello! I’ve found solution for Regular Expressions - Remove Whitespace from Start and End Challenge which I think is a little simpler than what is present as Solution in Guide Page. I’ve checked it with multiple words, devided by multiple whitespaces and I think it works as it should.
🌐
Mkyong
mkyong.com β€Ί home β€Ί java β€Ί java – how to remove spaces in between the string
Java - How to remove spaces in between the String - Mkyong.com
November 8, 2020 - String mysz = ” Hello Java β€œ; System.out.println(β€œ[β€œ+mysz.trim()+”]”); //Removes space from left and right –> [Hello Java] System.out.println(β€œ[β€œ+mysz.replaceAll(β€œ\W+”, ” β€œ)+”]”); //Removes space from middle –> [ Hello Java ] System.out.println(β€œ[β€œ+mysz.replaceAll(β€œ\W+”, ” β€œ).trim()+”]”); ////Removes space from middle, left and right–> [Hello Java] ... Thank you so very much. I always find exactly what I need on your website! ... Thank you so much. I’ve found many answers to solve errors in my codes on this website. ... Yes mkYong. You are right.Regular Expression(regex) is rock.
🌐
Flipper File
flipperfile.com β€Ί home β€Ί regex for whitespace cleanup (copy-ready patterns & examples)
Regex for Whitespace Cleanup (Copy-Ready Patterns & Examples) - Flipper File
December 5, 2025 - Use this when text contains too many spaces between words. Regex: \s{2,} Replace with: (single space) Example: Input: "This sentence has extra spaces." Output: "This sentence has extra spaces." Common in formatted emails, logs, and markdown. ... Great for cleaning up text before commit or export.
🌐
Reddit
reddit.com β€Ί r/sql β€Ί how to remove double spaces or more using regexp_replace
r/SQL on Reddit: How to remove double spaces or more using regexp_replace
August 28, 2021 -

Hi,

I am currently using regex_replace to get rid of some unwanted characters, but cannot figure out what to add to get it to remove double or triple spaces as well

What i'm currently doing is

regexp_replace(address, '[.,/()-]') as Address 

which works but when I try to add ' ' it does not get rid of double spaces, any suggestions?