107

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.

EDIT The string is being generated with ejs like so

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.

<?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> 
8
  • 1
    Try: /^[\s\n]+|[\s\n]+$/ Commented Jan 28, 2013 at 22:30
  • 9
    What trim do you use? The standard one does. Also, please show us your string, maybe you have some weird (invisible) characters in there. Commented Jan 28, 2013 at 22:31
  • 1
    Really Javascript trim() does not remove newlines? But it should remove all the whitespaces from the ends? And newline should be one of them. \n is a subelement of \s. Commented Jan 28, 2013 at 22:31
  • 1
    @elclanrs: linebreaks are included in \s Commented Jan 28, 2013 at 22:31
  • 2
    @LeeBlake trim() removed newline characters at the end of my String (bash output from execSync) so there must be something strange going on with his string. Commented Mar 25, 2016 at 18:18

4 Answers 4

148

Try this:

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

jsFiddle here.

Sign up to request clarification or add additional context in comments.

7 Comments

I solved it changing from this answer to str.replace(/^\n|\n$/g, '');
This also removes leading spaces.
How can we do the same thing with PHP?
Above example had a bug. This should work: str.replace(/(^\s*(?!.+)\n+)|(\n+\s+(?!.+)$)/g, "")
@phaberest, please write your own answer. It should be the accepted one.
|
134

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):

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


If you want to trim all newlines plus other potential whitespace, you can use the following:

return str.trim(); 

If you want to only trim newlines, you can use a solution that targets newlines specifically.

4 Comments

Maybe it was an IE issue? Looks like trim is only IE 9+, but heaven help you if you're supporting IE 8 at this point. ;^D
That's definitely a possibility :)
This is acceptable if you want to get rid of all whitespace... but if you need to just get rid of new lines and leave other white space sources this will fail.
@SpencerO'Reilly That's true, but the goal of the question is to remove newlines in addition to the other whitespace they were successfully removing. Removing only newlines can be helpful too, it's just not an answer to this question
7

/^\s+|\s+$/g should catch anything. Your current regex may have the problem that if your linebreaks contain \r characters they wouldn't be matched.

Comments

2

return str.trim().replace(/^\s\n+|\s\n+$/g,'')

if you use it like this then i will remove line break from start and end string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.