是否有 JavaScript 正则表达式可以删除除换行符以外的所有空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3871816/
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-23 06:27:05  来源:igfitidea点击:

Is there a JavaScript regular expression to remove all whitespace except newline?

javascriptregex

提问by sat

How do I remove white spaces in a string but not new line character in JavaScript. I found a solution for C# , by using \t?, but it's not supported in JavaScript.

如何删除字符串中的空格而不是 JavaScript 中的换行符。我通过使用找到了 C# 的解决方案\t?,但它在 JavaScript 中不受支持。

To make it more clear, here's an example:

为了更清楚,这里有一个例子:

var s = "this\n is a\n te st"

using regexp method I expect it to return

使用正则表达式方法我希望它返回

"this\nisa\ntest"

采纳答案by Ruel

This will work, even on \t.

这将起作用,即使在\t.

var newstr = s.replace(/ +?/g, '');

回答by tKe

Although in Javascript / /gdoes match \t, I find it can hide the original intent as it reads as a match for the space character. The alternative would be to use a character collection explicitly listing the whitespace characters, excluding \n. i.e. /[ \t\r]+/g.

尽管在 Javascript 中/ /g确实 match \t,但我发现它可以隐藏原始意图,因为它读取为空格字符的匹配项。另一种方法是使用显式列出空白字符的字符集合,不包括\n. 即/[ \t\r]+/g

var newString = s.replace(/[ \t\r]+/g,"");

回答by Steven Vachon

[^\S\r\n]+

Not a non-whitespace char, not \rand not \n; one or more instances.

不是空白字符,不是\r和不是\n;一个或多个实例。

回答by Matthew Crumley

If you want to match every whitespace character that \smatches except for newlines, you could use this:

如果要\s匹配除换行符以外的每个匹配的空白字符,可以使用以下命令:

/[\t\v\f\r \u00a0\u2000-\u200b\u2028-\u2029\u3000]+/g

Note that this will remove carriage returns (\r), so if the input contains \r\npairs, they will be converted to just \n. If you want to preserve carriage returns, just remove the \rfrom the regular expression.

请注意,这将删除回车符 ( \r),因此如果输入包含\r\n对,它们将被转换为仅\n. 如果要保留回车符,只需\r从正则表达式中删除。

回答by Chinmayee G

Try this

尝试这个

var trimmedString = orgString.replace(/^\s+|\s+$/g, '') ;

回答by shoesel

This does the trick:

这是诀窍:

str.replace(/ /g, "")

and the space does NOT match tabs or linebreaks (CHROME45), no plus or questionmark is needed when replacing globally.

并且空格不匹配制表符或换行符 (CHROME45),全局替换时不需要加号或问号。

In Perl you have the "horizontal whitespace" shorthand \h to destinguish between linebreaks and spaces but unfortunately not in JavaScript.

在 Perl 中,您有“水平空白”速记 \h 来区分换行符和空格,但不幸的是在 JavaScript 中没有。

The \t shorthand on the other hand IS supported in JavaScript, but it describes the tabulator only.

另一方面,JavaScript 支持 \t 速记,但它仅描述制表符。

回答by Dávid Konkoly

const str = "abc def ghi"; 

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

-> "abcdefghi"

回答by Mattijs

code.replace(/^\s[^\S]*/gm, '')

code.replace(/^\s[^\S]*/gm, '')

works for me on text like:

对我有用,例如:

    #set($todayString = $util.time.nowEpochMilliSeconds())
    #set($pk = $util.autoId())
    $util.qr($ctx.stash.put("postId", $pk))

and removes the space/tabs before the first 3 lines with removing the spaces in the line.

并删除前 3 行之前的空格/制表符,并删除该行中的空格。

enter image description here

在此处输入图片说明

*optimisation by @Toto: code.replace(/^\s+/gm, '')

*@Toto 优化: code.replace(/^\s+/gm, '')

回答by ArK

try this '/^\\s*/'

尝试这个 '/^\\s*/'