从字符串的开头和结尾删除换行符 [英] Remove line breaks from start and end of string

查看:155
本文介绍了从字符串的开头和结尾删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到trim()不会从字符串的开头和结尾删除换行符,所以我试图用以下正则表达式完成此操作:

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:

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.

编辑
字符串是使用类似的ejs生成的

EDIT The string is being generated with ejs like so

go = ejs.render(data, { 
    locals: { 
        format() {
            // 
        }
    } 
});

这就是现在的情况,但前面有一些空行。当我使用go.trim()时,我仍然得到之前的新行。

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.

<?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>


推荐答案

试试这个:

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

jsFiddle在这里

这篇关于从字符串的开头和结尾删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆