Javascript Regular Expression Remove Spaces
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here
(function($) {
$.stripSpaces = function(str) {
var reg = new RegExp("[ ]+","g");
return str.replace(reg,"");
}
})(jQuery);
my regular expression is currently [ ]+ to collect all spaces.
This works.. however It doesn't leave a good taste in my mouth..
I also tried [s]+ and [W]+ but neither worked..
There has to be a better (more concise) way of searching for only spaces.
javascript regex
add a comment |
So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here
(function($) {
$.stripSpaces = function(str) {
var reg = new RegExp("[ ]+","g");
return str.replace(reg,"");
}
})(jQuery);
my regular expression is currently [ ]+ to collect all spaces.
This works.. however It doesn't leave a good taste in my mouth..
I also tried [s]+ and [W]+ but neither worked..
There has to be a better (more concise) way of searching for only spaces.
javascript regex
add a comment |
So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here
(function($) {
$.stripSpaces = function(str) {
var reg = new RegExp("[ ]+","g");
return str.replace(reg,"");
}
})(jQuery);
my regular expression is currently [ ]+ to collect all spaces.
This works.. however It doesn't leave a good taste in my mouth..
I also tried [s]+ and [W]+ but neither worked..
There has to be a better (more concise) way of searching for only spaces.
javascript regex
So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here
(function($) {
$.stripSpaces = function(str) {
var reg = new RegExp("[ ]+","g");
return str.replace(reg,"");
}
})(jQuery);
my regular expression is currently [ ]+ to collect all spaces.
This works.. however It doesn't leave a good taste in my mouth..
I also tried [s]+ and [W]+ but neither worked..
There has to be a better (more concise) way of searching for only spaces.
javascript regex
javascript regex
asked Aug 22 '11 at 17:28
rlemonrlemon
13.8k1078116
13.8k1078116
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
I would recommend you to use the literal notation, and using 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 of more white-space characters, for example 'trn' 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 worked 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)).
add a comment |
"foo is bar".replace(/ /g, '')
add a comment |
str.replace(/s/g,'')
Works for me.
jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:
// Check if a string has a non-whitespace character in it
rnotwhite = /S/
// IE doesn't match non-breaking spaces with s
if ( rnotwhite.test( "xA0" ) ) {
trimLeft = /^[sxA0]+/;
trimRight = /[sxA0]+$/;
}
add a comment |
This works just as well: http://jsfiddle.net/maniator/ge59E/3/
var reg = new RegExp(" ","g"); //<< just look for a space.
add a comment |
In production
This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.
text.replace(/[nrst]+/g, ' ')
add a comment |
Remove all spaces in string
// Remove only spaces
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/ /g,'');
"
Textwithspaces1111
andsome
breaklines
"
// Remove spaces and breaklines
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/s/g,'');
"Textwithspaces1111andsomebreaklines"
add a comment |
protected by bummi Dec 11 '14 at 19:44
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would recommend you to use the literal notation, and using 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 of more white-space characters, for example 'trn' 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 worked 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)).
add a comment |
I would recommend you to use the literal notation, and using 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 of more white-space characters, for example 'trn' 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 worked 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)).
add a comment |
I would recommend you to use the literal notation, and using 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 of more white-space characters, for example 'trn' 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 worked 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 to use the literal notation, and using 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 of more white-space characters, for example 'trn' 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 worked 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)).
answered Aug 22 '11 at 17:34
CMSCMS
603k163848815
603k163848815
add a comment |
add a comment |
"foo is bar".replace(/ /g, '')
add a comment |
"foo is bar".replace(/ /g, '')
add a comment |
"foo is bar".replace(/ /g, '')
"foo is bar".replace(/ /g, '')
answered Aug 22 '11 at 17:31
Ransom BriggsRansom Briggs
1,71732643
1,71732643
add a comment |
add a comment |
str.replace(/s/g,'')
Works for me.
jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:
// Check if a string has a non-whitespace character in it
rnotwhite = /S/
// IE doesn't match non-breaking spaces with s
if ( rnotwhite.test( "xA0" ) ) {
trimLeft = /^[sxA0]+/;
trimRight = /[sxA0]+$/;
}
add a comment |
str.replace(/s/g,'')
Works for me.
jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:
// Check if a string has a non-whitespace character in it
rnotwhite = /S/
// IE doesn't match non-breaking spaces with s
if ( rnotwhite.test( "xA0" ) ) {
trimLeft = /^[sxA0]+/;
trimRight = /[sxA0]+$/;
}
add a comment |
str.replace(/s/g,'')
Works for me.
jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:
// Check if a string has a non-whitespace character in it
rnotwhite = /S/
// IE doesn't match non-breaking spaces with s
if ( rnotwhite.test( "xA0" ) ) {
trimLeft = /^[sxA0]+/;
trimRight = /[sxA0]+$/;
}
str.replace(/s/g,'')
Works for me.
jQuery.trim has the following hack for IE, although I'm not sure what versions it affects:
// Check if a string has a non-whitespace character in it
rnotwhite = /S/
// IE doesn't match non-breaking spaces with s
if ( rnotwhite.test( "xA0" ) ) {
trimLeft = /^[sxA0]+/;
trimRight = /[sxA0]+$/;
}
answered Aug 22 '11 at 17:31
Stefan KendallStefan Kendall
41.7k56213374
41.7k56213374
add a comment |
add a comment |
This works just as well: http://jsfiddle.net/maniator/ge59E/3/
var reg = new RegExp(" ","g"); //<< just look for a space.
add a comment |
This works just as well: http://jsfiddle.net/maniator/ge59E/3/
var reg = new RegExp(" ","g"); //<< just look for a space.
add a comment |
This works just as well: http://jsfiddle.net/maniator/ge59E/3/
var reg = new RegExp(" ","g"); //<< just look for a space.
This works just as well: http://jsfiddle.net/maniator/ge59E/3/
var reg = new RegExp(" ","g"); //<< just look for a space.
answered Aug 22 '11 at 17:29
NealNeal
114k31214274
114k31214274
add a comment |
add a comment |
In production
This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.
text.replace(/[nrst]+/g, ' ')
add a comment |
In production
This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.
text.replace(/[nrst]+/g, ' ')
add a comment |
In production
This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.
text.replace(/[nrst]+/g, ' ')
In production
This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.
text.replace(/[nrst]+/g, ' ')
answered Jan 13 at 16:28
Jason SebringJason Sebring
13.4k66264
13.4k66264
add a comment |
add a comment |
Remove all spaces in string
// Remove only spaces
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/ /g,'');
"
Textwithspaces1111
andsome
breaklines
"
// Remove spaces and breaklines
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/s/g,'');
"Textwithspaces1111andsomebreaklines"
add a comment |
Remove all spaces in string
// Remove only spaces
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/ /g,'');
"
Textwithspaces1111
andsome
breaklines
"
// Remove spaces and breaklines
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/s/g,'');
"Textwithspaces1111andsomebreaklines"
add a comment |
Remove all spaces in string
// Remove only spaces
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/ /g,'');
"
Textwithspaces1111
andsome
breaklines
"
// Remove spaces and breaklines
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/s/g,'');
"Textwithspaces1111andsomebreaklines"
Remove all spaces in string
// Remove only spaces
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/ /g,'');
"
Textwithspaces1111
andsome
breaklines
"
// Remove spaces and breaklines
`
Text with spaces 1 1 1 1
and some
breaklines
`.replace(/s/g,'');
"Textwithspaces1111andsomebreaklines"
edited Jan 3 at 22:21
answered Dec 12 '18 at 20:57
DarckBlezzerDarckBlezzer
2,04612130
2,04612130
add a comment |
add a comment |
protected by bummi Dec 11 '14 at 19:44
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?