What is a regular expression to remove spaces between uppercase letters, but spaces between words?

For example, if I have a string like "Hello I BM", how can I define a space between uppercase letters, but not between "o" and "I"?

In principle, "Hello I BM" should enable "Hello IBM"

So far I have this:

value = "Hello IBM" value = value.replace(/([AZ])\s([AZ])/g, '$1$2') 

But it only replaces the first instance of a space between two uppercase letters, such as: "Hello IB M"

- EDIT -

Solution Part 1:

  value = value.replace(/([AZ])\s(?=[AZ])/g, '$1') 

Thanks Renato for the first part of the solution! They just found out if there is a headword AFTER A WRITING RECORD, it swallows this space. How do we save space there?

So, "Hello I BM Dude" becomes "Hello IBMDude" instead of "Hello IBM Dude"

+6
source share
1 answer

When the regular expression matches the first (on "AB" ), this part of the string is consumed by the engine, so it doesn't match again, although your regular expression has a global flag ( 'g' ).

You can achieve the expected result by using positive lookahead ( (?=PATTERN) ) instead, which will not consume a match:

 value = "Hello IBM" value = value.replace(/([AZ])\s(?=[AZ])/g, '$1') console.log(value) // Prints "Hello IBM" 

To do this, do not remove the space if the next letter in uppercase is the first in the word, you can enlarge the lookahead pattern with the \b border of the word to make this restriction:

 value = "Hello IBM Dude" value = value.replace(/([AZ])\s(?=[AZ]\b)/g, '$1') console.log(value) // Prints "Hello IBM Dude" 

Note As @CasimirHyppolite noted, the next letter must be optional, or the second regular expression will not work if the last character of the line is uppercase. Thus, the pattern ([^A-Za-z]|$) , which can be considered "not the letter or the end of the line."

Edit : simplify lookahead from (?=[AZ]([^A-Za-z]|$)) to (?=[AZ]\b) as suggested by @hwnd

+9
source

Source: https://habr.com/ru/post/957464/


All Articles