1

I'm using the following JS code to replace one or more consecutive whitespace characters with a single space character:

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

I would like to modify the regex to ignore non-breaking spaces.

For example, this:

ABC   \u00a0\u00a0XYZ

Should result in this:

ABC \u00a0\u00a0XYZ
robinCTS
  • 5,746
  • 14
  • 30
  • 37
  • Do you mean to also replace the non-breaking spaces? If not, please clarify what you mean by 'skipping the non-breaking spaces'. Ideally give some examples. – Peter B Mar 16 '18 at 22:06
  • The non-breaking spaces shouldn't be replaced – Dario Quiroga Mar 16 '18 at 22:10
  • This has been discussed a lot of times. `/(?:(?!\u00A0)\s)+/g` or `/[^\S\u00A0]+/g` will do. – Wiktor Stribiżew Mar 16 '18 at 22:14
  • @ivan-pozdeev *Exclude characters from a character class doesn't say anything about handling Unicode characters, nor how to match "any whitespace character except a specific one".* - Of course not. It uses `\w` as an example **for the general case**. It's like me pointing you to a duplicate question which asks "what happens if I add 1 to a number" and the answer is "let's take 2, for example - the result is the next number to the right along the number line, i.e., 3". What you're saying is the equivalent of "but that answer doesn't help me as the number in my question is not 2". Seriously!? – robinCTS Mar 17 '18 at 05:27

0 Answers0