STRING.replaceAll("\r", "").replaceAll("\n", "");
- To remove HTML line breaks –
STRING.replaceAll("<br>", "").replaceAll("<br/>", "");
- To remove only leading and trailing line breaks –
That should cover the basics, but if you need more concrete examples – Read on!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
REAL QUICK SLIDES
TABLE OF CONTENTS
|Download & Notes||Remove Line Break||Useful Bits & Links|
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
REMOVE LINE BREAKS
1) REGULAR EXPRESSION
// (A) THE STRING var str = "Foo\r\nGoo\r\nBar"; console.log(str); // (B) STRIP FIRST LINE BREAK var nobreak = str.replace(/\r?\n|\r/, ""); console.log(nobreak); // FooGoo\r\nBar // (C) STRIP ALL LINE BREAKS var nobreak = str.replace(/\r?\n|\r/g, ""); console.log(nobreak); // FooGooBar
This is probably the most common method that everyone on the Internet is recommending – Myself included. Regular expressions is a big can of worms, so I will just leave
replace(/\r?\n|\r/, "") as “the ways to remove all line breaks”.
// (A) THE STRING var str = "Foo\r\nGoo\r\nBar"; console.log(str); // (B) STRIP ALL LINE BREAKS var nobreak = str.replaceAll("\r", "").replaceAll("\n", ""); console.log(nobreak); // FooGooBar
This is an alternate way to strip line breaks – By replacing all
\n with an empty string.
3) REMOVE HTML LINE BREAKS
// (A) THE STRING var str = "Foo<br>Goo<br>Bar"; console.log(str); // (B) STRIP ALL LINE BREAKS var nobreak = str.replaceAll("<br>", "").replaceAll("<br/>", ""); console.log(nobreak); // FooGooBar
Captain Obvious to the rescue! Line breaks in HTML are added with the
<br> (or some people may prefer
<br/>) tag. We just remove them using the same old
// (A) THE STRING var str = "\r\nFoo\r\nGoo\r\n"; console.log(str); // (B) STRIP LEADING & TRAILING LINE BREAKS var nobreak = str.trim(); console.log(nobreak); // Foo\r\nGoo
If you just want to remove the line breaks from the start and end of the string – Just use
USEFUL BITS & LINKS
That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.
EXTRA) MYSTERY OF LINE BREAKS
Some of you curious code ninjas may be wondering, what’s up with the
\rRepresents a carriage return (hex code 0D)
\nRepresents a newline (hex code 0A)
Yep, both of them kind of represents the same “newline” or “line break”. Then comes the interesting part:
- Linux uses
- Mac uses
- Windows uses
No need to worry too much though. We code ninjas simply use both
\r\n, and strip the both of them regardlessly.
INFOGRAPHIC CHEAT SHEET
LINKS & REFERENCES
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!