admin管理员组文章数量:1135140
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.
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.
Share Improve this question asked Aug 22, 2011 at 17:28 rlemonrlemon 17.7k14 gold badges94 silver badges126 bronze badges7 Answers
Reset to default 305I 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, '')
In production and works across line breaks
This is used in several apps to clean user-generated content removing extra spacing/returns etc but retains the meaning of spaces.
text.replace(/[\n\r\s\t]+/g, ' ')
This works just as well: http://jsfiddle.net/maniator/ge59E/3/
var reg = new RegExp(" ","g"); //<< just look for a space.
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 = /^[\s\xA0]+/;
trimRight = /[\s\xA0]+$/;
}
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"
Both of them should work:
text.replace(/ +/g,' ')
Or:
text.replace(/ {2,}/g, ' ')
const text = "eat healthy and drink gallon of water."
text.replace(/ +/g,' ')
// eat healthy and drink gallon of water.
text.replace(/ {2,}/g, ' ')
// eat healthy and drink gallon of water.
本文标签: regexJavascript Regular Expression Remove SpacesStack Overflow
版权声明:本文标题:regex - Javascript Regular Expression Remove Spaces - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736790055a1953047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论