I would recommend you use the literal notation, and the \s character class:

//..
return str.replace(/\s/g, '');
//..

There's a difference between using the character class \s and just ' ', this will match a lot more white-space characters, for example '\t\r\n' etc.., looking for ' ' will replace only the ASCII 32 blank space.

The RegExp constructor is useful when you want to build a dynamic pattern, in this case you don't need it.

Moreover, as you said, "[\s]+" didn't work with the RegExp constructor, that's because you are passing a string, and you should "double escape" the back-slashes, otherwise they will be interpreted as character escapes inside the string (e.g.: "\s" === "s" (unknown escape)).

Answer from Christian C. Salvadó on Stack Overflow
🌐
Dot Net Perls
dotnetperls.com › regex-replace-spaces
C# - Regex.Replace Spaces Example - Dot Net Perls
Regex.Replace can act upon spaces. It can merge multiple spaces in a string to one space.
Discussions

Replacing space and comma from a string using regex
I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3
0
October 26, 2020
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
How to replace underscores with spaces using a regex in Javascript - Stack Overflow
How can I replace underscores with spaces using a regex in Javascript? var ZZZ = "This_is_my_name"; More on stackoverflow.com
🌐 stackoverflow.com
Regex to replace multiple spaces with a single space
Given a string like: "The dog has a long tail, and it is RED!" What kind of jQuery or JavaScript magic can be used to keep spaces to only one space max? Goal: "The dog has a long t... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Make Community
community.make.com › questions
Regex removing , space and replacing with , - Questions - Make Community
May 9, 2023 - Hi, I am using the Text Replace Module to help remove the SPACE after a comma in a string of text… Below is my module I have a literal space after the , in Pattern… I also tried Regex: /,\s?/ The regex isn’t work…
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 20956 › regex-search-and-replace-remove-all-kind-of-spaces-from-the-beginning-of-the-line
Regex: Search and Replace / Remove all kind of spaces from the beginning of the line | Notepad++ Community
March 29, 2021 - For instance, when inserted in replacement, the regex ^\s+\S+ \1\2 would simply replace the overall search match with the string ^s+S+ followed with a space char and the contents of the search groups 1 and 2 !
🌐
freeCodeCamp
forum.freecodecamp.org › programming › javascript
Replacing space and comma from a string using regex
October 26, 2020 - I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ...
🌐
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 can sort that out with this piece of Regex (goes into a StringReplacer transformer): ... Just make sure that the Case Sensitive parameter is set correctly. But still, that only works when the string is in Title Case, not UPPER CASE. But you could go one step further and try to put spaces back where we think they should be. For example, we could follow up with a second StringReplacer that replaces "LANE" with "<space>LANE", like so:
Find elsewhere
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 25216 › proper-syntax-for-s-spaces-in-replace-expression
Proper syntax for \s (spaces) in replace expression | Notepad++ Community
December 8, 2023 - If you want to restrict the search to just horizontal whitespace (like space or tab), then use \h instead of \s. And if you want to restrict the search to just ASCII 32 space characters, then use \x20 in the FIND just like @ Alan-Kilborn suggested ...
🌐
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,…
🌐
Techstacker
techstacker.com › how-to-replace-whitespace-javascript-regex
How to Replace White Space inside Strings with JavaScript ...
June 8, 2020 - The DEMO version only includes 4 pages: http://techstacker.com http://techstacker.com/embed_ext_iframe.html http://techstacker.com/topics.html http://techstacker.com/tags/html.html It is possible to download these free files and install them on your server, so you can test how the site works.
🌐
Laserfiche Answers
answers.laserfiche.com › questions › 52560 › Regular-expression-to-remove-white-space
Regular expression to remove white space - Laserfiche Answers
March 17, 2014 - In this case, you should look to use a \s to indicate that you're expecting a "white space character". So the full regular expression would be (\w+)\s(\w).
🌐
Regular-Expressions.info
regular-expressions.info › examples.html
Regular Expression Examples
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). Search for [ \t]+$ to trim trailing whitespace.
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › Regex-to-replace-every-character-with-a-space › td-p › 984007
Solved: Regex to replace every character with a space - Alteryx Community
June 13, 2024 - Hello All, Odd request this and my regex is failing me. I need to take a column and basically have the text string have as many " " as it does letters. (Appreciating this will be an empty field full of dynamic spaces depending on cell string length). I did try REGEX_Replace([My data],"\.", " ") b...
🌐
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 - In this solution, any sequence of whitespace characters (line breaks, tabs, spaces, etc.) is replaced with a single space. Since the ‹+› quantifier repeats the ‹\s› whitespace class one or more times, even a single tab character, for example, will be replaced with a space.
Authors   Jan GoyvaertsSteven Levithan
Published   2012
Pages   609
🌐
Sabe
sabe.io › blog › javascript-replace-whitespace-regex
How to Replace White Space in Strings using Regex in JavaScript | Sabe
December 5, 2022 - The regular expression we used is /\\s/g which matches all the white space in the string. The global flag g is used to replace all the white space in the string, instead of just the first match.