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)).
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)).
"foo is bar".replace(/ /g, '')
Replacing space and comma from a string using regex
Regular Expressions - Remove Whitespace from Start and End | Simpler solution? - freeCodeCamp Community - The freeCodeCamp Forum
How to replace underscores with spaces using a regex in Javascript - Stack Overflow
Regex to replace multiple spaces with a single space
How do I replace any number of spaces using regular expressions
Notepad++ Solution
To match one or more space characters:
- Set "Find what" to
+(space followed by +)
To match one of more whitespace characters (space, EOL, and tab all count as whitespace):
Set "Find what" to
\s+Warning: Using
\s+will match end of line and therefore join multiple lines together (separated by the "replace with" string)
To replace with a tab character:
- Set "Replace with" to
\t
To enable regular expression (so the above special codes will work)
- Select "Regular expression".

Source How to use regular expressions in Notepad++ (tutorial)
Taken from here:
Use as "find" expression:
{1,}
namely a space followed by {1,}.
To replace with tab, enter ^t in the replace box. Don't forget to activate regular expressions.
This link covers the syntax of the given regex. Below is an extract of a relevant part.
{n,} Matches when the preceding character occurs at least n times, for example, ba{2,}b will find 'baab', 'baaab' or 'baaaab' but NOT 'bab'. Values are enclosed in braces (curly brackets).
For the records, it has been tested on notepad++ (See here, courtesy of barlop). You can also put a \t in the replace box.
Depending on your regex flavour, you can do:
- Find:
(?:^NAME : |\G(?!^))\w+\K\h - Replace:
_
A demo and explanation can be found here on Regex101.
It depends on where you're using the regex:
The following uses sed:
sed ':redo s/^\(\s*NAME\s*:\s*\S\+\)\s\+\([^_[:space:]]\)/\1_\2/ ;t redo' files
The trick here is the t redo, which does a conditional branch back to the label redo, to rerun the replacement until it fails.
Note that the pattern includes some glue to make spaces around the : optional, ignore trailing white space and reduce multiple spaces to a single _.
If it is a JavaScript code, write this, to have transformed string in ZZZ2:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.replace(/_/g, " ");
also, you can do it in less efficient, but more funky, way, without using regex:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.split("_").join(" ");
Regular expressions are not a tool to replace texts inside strings but just something that can search for patterns inside strings. You need to provide a context of a programming language to have your solution.
I can tell you that the regex _ will match the underscore but nothing more.
For example in Groovy you would do something like:
"This_is_my_name".replaceAll(/_/," ")
===> This is my name
but this is just language specific (replaceAll method)..
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.)
I don't know what language you plan on using this in, but you can replace this pattern:
[^\d]+, with an empty string should accomplish this. It'll remove everything that's not a number.
Using PCRE regexes, you should be able to simply remove anything matching \D+. Example:
echo "+jfalkjfkl saj f62 81 7876 asdadad30 asasda36" | perl -pe 's/\D+//g'
prints:
628178763036
