JavaScript Remove Carriage Return and Line Feed

JavaScript Remove Carriage Return and Line Feed | Carriage Return (CR, ASCII character 13) is represented by ‘\r’, and line feed by ‘\n’ (LF, ASCII character 10). In the past, you could instruct a printer with two ASCII characters at the end of each line: CR would direct the printer to return to the left side of the page, and LF would direct the printer to move on to the next line. The digital equivalents of how a typewriter would advance to the next line are ‘\r’ and ‘\n’.

The ‘\r’ stands for carriage return and ‘\n’ for newline (also known as a linefeed). On a typewriter, you would move the carriage back to the leftmost position and then feed the paper up a line to get to the beginning of the new line.

Windows uses \r \n, Macs used r prior to OS 9, while Unix uses n to denote a new line. Operating systems still use a variety of standards for indicating the end of a line; some use \n \r, others use \n, and some use \r \n.

In JavaScript, we usually work with the \n character, which is used to switch strings to the next line. However, we can also run into difficulties depending on the strings we are working with. Text line breaks are typically indicated by one of three characters: \r, \n, or \r. 

We must deal with all three forms of line breaks since your text may typically originate from any of those sources in order to remove line feed and carriage return from texts.

JavaScript Remove Carriage Return and Line Feed using replace()

The replace() technique in JavaScript locates a pattern and substitutes a replacement character/string for any or all of its occurrences. Characters, strings, or regExp can all be used as the pattern.

The regular expression object is called RegExp. These items are patterns that are used to compare string character sequences.

let text = "Some text here!!\n" +
    "This line break is going to be removed!!! \r using JavaScript.\r\n";
let stringWithoutLineBreaks = text.replace(/(\r\n|\n|\r)/gm, '')
console.log("Original String: " + text)
console.log("Final String: " + stringWithoutLineBreaks)

Output:-

Original String: Some text here!!
using JavaScript. going to be removed!!!

Final String: Some text here!!This line break is going to be removed!!! using JavaScript.

We are utilizing the replace() technique with RegExp in the code above. The replace() method’s first input, a regular expression, directs it to replace any line breaks it encounters in the string it is applying to. The replacement, in this case, nothing (“), is the second argument. Where “n” denotes “line feed” and “r” denotes “carrier return”.

JavaScript Remove Carriage Return and Line Feed Using split() & join()

Javascript’s split() method produces an array of substrings from a supplied text. A string is created by joining the array’s items together using Javascript’s join() technique.

The code below demonstrates how we split the original string using the split() method into an array of substrings based on carriage return ‘r’ and line breaks ‘n’. Use the join() method to rejoin the array into a single string.

let text = "Some text here!!\n " +
    "This line break is going to be removed!!! \r using JavaScript.\r\n";
let stringWithoutLineBreaks =  text.split('\r').join('').split('\n').join('')
console.log("Original String: " + text)
console.log("Final String: " + stringWithoutLineBreaks)

Output:-

Original String: Some text here!!
using JavaScript. going to be removed!!!

Final String: Some text here!!This line break is going to be removed!!! using JavaScript.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *