我注意到trim()不能删除字符串开头和结尾处的换行符,因此我正在尝试使用以下正则表达式来实现这一点:
我注意到trim()不能删除字符串开头和结尾处的换行符,因此我正在尝试使用以下正则表达式来实现这一点:
return str.replace(/^\s\n+|\s\n+$/g,'');
这并不会去除换行符,我担心我在这方面的能力有限。
编辑 该字符串是使用ejs生成的,如下所示
go = ejs.render(data, {
locals: {
format() {
//
}
}
});
这就是 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>
trim
函数?标准的那个会用。另外,请展示你的字符串,或许里面包含一些奇怪(看不见的)字符。 - BergiJavascript
中的trim()
不能去除newlines
吗?但它应该可以从末尾删除所有的whitespaces
,而newline
应该是其中之一。\n
是\s
的子元素之一。 - Rohit Jain\s
中。 - Bergitrim()
函数删除了我的字符串末尾的换行符(来自execSync的bash输出),因此他的字符串可能存在一些奇怪的问题。 - Joshua Pintertrim()
确实会去除空格、制表符、\n
、\r
等。这在2009年(ES5)被标准化。所以如果你记得它不会去除换行符,你可能和我一样老了。 - undefined