Javascript Javascript正则表达式删除空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7151159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:03:46  来源:igfitidea点击:

Javascript Regular Expression Remove Spaces

javascriptregex

提问by rlemon

So i'm writing a tiny little plugin for JQuery to remove spaces from a string. see here

所以我正在为 JQuery 编写一个小插件来从字符串中删除空格。看这里

(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..

我的正则表达式目前[ ]+是收集所有空格。这有效..但是它不会在我嘴里留下好味道..我也尝试过[\s]+[\W]+但都没有奏效..

There has to be a better (more concise) way of searching for only spaces.

必须有一种更好(更简洁)的方法来仅搜索空格。

回答by CMS

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

我建议您使用文字符号和\s字符类:

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

There's a difference between using the character class \sand 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.

使用字符类\s和 just之间存在差异' ',这将匹配更多的空白字符,例如'\t\r\n'等等,查找' '将仅替换 ASCII 32 空格。

The RegExpconstructor is useful when you want to builda dynamic pattern, in this case you don't need it.

RegExp当你想构造是有用的建立一个动态的模式,在这种情况下,你不需要它。

Moreover, as you said, "[\s]+"didn't work with the RegExpconstructor, 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)).

此外,正如你所说,"[\s]+"没有与RegExp构造函数一起工作,那是因为你正在传递一个字符串,你应该“双重转义”反斜杠,否则它们将被解释为字符串内的字符转义(例如:("\s" === "s"未知)逃脱))。

回答by Ransom Briggs

"foo is bar".replace(/ /g, '')

回答by Stefan Kendall

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

Works for me.

为我工作。

jQuery.trimhas the following hack for IE, although I'm not sure what versions it affects:

jQuery.trim对 IE 有以下 hack,虽然我不确定它会影响哪些版本:

// 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]+$/;
}

回答by DarckBlezzer

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"

回答by Jason Sebring

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, ' ')

回答by Naftali aka Neal

This works just as well: http://jsfiddle.net/maniator/ge59E/3/

这也适用:http: //jsfiddle.net/maniator/ge59E/3/

var reg = new RegExp(" ","g"); //<< just look for a space.