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
What is a regular expression for removing spaces between uppercase letters, but keeps spaces between words?
Strip all white spaces between words
Remove spaces from string if consecutive one letter characters or numbers - Statalist
Regular Expressions - Remove Whitespace from Start and End | Simpler solution? - freeCodeCamp Community - The freeCodeCamp Forum
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 newlinedepending 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
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.
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, ' ');
Since you seem to be interested in performance, I profiled these with firebug. Here are the results I got:
str.replace( / +/g, ' ' ) -> 380ms
str.replace( /\s\s+/g, ' ' ) -> 390ms
str.replace( / {2,}/g, ' ' ) -> 470ms
str.replace( / +/g, ' ' ) -> 790ms
str.replace( / +(?= )/g, ' ') -> 3250ms
This is on Firefox, running 100k string replacements.
I encourage you to do your own profiling tests with firebug, if you think performance is an issue. Humans are notoriously bad at predicting where the bottlenecks in their programs lie.
(Also, note that IE 8's developer toolbar also has a profiler built in -- it might be worth checking what the performance is like in IE.)
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?