Javascript 从字符串的开头和结尾删除换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14572413/
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
Remove line breaks from start and end of string
提问by Jen Zhang
I noticed that trim() does not remove new line characters from the start and end of a string, so I am trying to accomplish this with the following regex:
我注意到 trim() 不会从字符串的开头和结尾删除换行符,因此我尝试使用以下正则表达式来完成此操作:
return str.replace(/^\s\n+|\s\n+$/g,'');
This does not remove the new lines, and I fear I am out of my depth here.
这不会删除新行,我担心我在这里超出了我的深度。
EDITThe string is being generated with ejs like so
编辑字符串是用 ejs 生成的
go = ejs.render(data, {
locals: {
format() {
//
}
}
});
And this is what go is, but with a few empty lines before. When I use go.trim() I still get the new lines before.
这就是 go 的内容,但之前有一些空行。当我使用 go.trim() 时,我仍然会得到新的线条。
<?xml version="1.0"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="Out" page-width="8.5in" page-height="11in" margin-top="1in" margin-bottom="0.5in" margin-left="0.75in" margin-right="0.75in">
<fo:region-body margin-top="1in" margin-bottom="0.25in"/>
<fo:region-before extent="1in"/>
<fo:region-after extent="0.25in"/>
<fo:region-start extent="0in"/>
<fo:region-end extent="0in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="Out" initial-page-number="1" force-page-count="no-force">
<fo:static-content flow-name="xsl-region-before">
<fo:block font-size="14pt" text-align="center">ONLINE APPLICATION FOR SUMMARY ADVICE</fo:block>
<fo:block font-size="13pt" font-weight="bold" text-align="center">Re:
SDF, SDF
</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body" font="10pt Helvetica">
.. removed this content
</fo:flow>
</fo:page-sequence>
</fo:root>
回答by hohner
回答by Ben Kane
String.trim()does in fact remove newlines (and all other whitespace). Maybe it didn't used to? It definitely does at the time of writing. From the linked documentation (emphasis added):
String.trim()实际上确实删除了换行符(和所有其他空格)。也许它不习惯?在撰写本文时它确实如此。从链接的文档(强调):
The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
trim() 方法从字符串的两端删除空格。此上下文中的空白是所有空白字符(空格、制表符、不间断空格等)和所有行终止符(LF、CR 等)。
If you want to trim all newlines plus other potential whitespace, you can use the following:
如果要修剪所有换行符和其他潜在的空格,可以使用以下命令:
return str.trim();
If you want to onlytrim newlines, you can use a solution that targets newlines specifically.
如果你想只修剪换行符,您可以使用一个解决方案,目标明确换行符。
回答by Bergi
/^\s+|\s+$/gshould catch anything. Your current regex may have the problem that if your linebreaks contain \rcharacters they wouldn't be matched.
/^\s+|\s+$/g应该抓住任何东西。您当前的正则表达式可能存在的问题是,如果您的换行符包含\r无法匹配的字符。
回答by yesmeck
Try this:
尝试这个:
str.split('\n').join('');

