While developing in any language, there is a common requirement to remove all the line breaks from a string. This article will talk about removing all the line breaks from a javascript string using different methods and example illustrations. The article also shows how to remove line breaks from the start and end of a javascript string.

Table of Contents:-

Javascript string remove all line breaks using replace() and RegExp

Javascript’s replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

RegExp is the regular expression object. Theses objects are patterns used to match character combinations in strings.

Example:-

Remove all line breaks from the string “\n Javascript \nis a popular \nlanguage \n”

Code:-

let dummyString = " \n Javascript \nis a popular \nlanguage \n"
let stringWithoutLineBreaks = dummyString.replace(/(\r\n|\n|\r)/gm, '')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringWithoutLineBreaks)

Here in the above code, we are using replace() method with RegExp. The regular expression is the first argument stating that this replace() method should replace all line breaks found in the string on which the method is applied. The second argument is the replacement, nothing (”) in our case.

Where \n is for line feed and \r for carriage return

Output:-

Original String:  
 Javascript 
is a popular 
language 

Final String:   Javascript is a popular language 

Javascript string remove all line breaks using replaceAll()

Javascript’s replaceAll() method will replace all the pattern matches within a string with a replacement. The first argument is a pattern which can be a string or a RegExp. This first argument is to be searched in string and replaced with a replacement. The second argument is a replacement which can be a string or a function.

Example:-

Remove all line breaks from the string “\n Javascript \nis a popular \nlanguage \n”

Code:-

Here the below code uses the replaceAll() method to replace all the occurrences of the line break. Line feed (\n) is passed as the first argument and a replacement is passed as the second argument, nothing (”) in our case. 

let dummyString = " \n Javascript\n is a popular \nlanguage \n"
let stringWithoutLineBreaks = dummyString.replaceAll('\n','')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringWithoutLineBreaks)

Output:-

Original String:  
 Javascript 
is a popular 
language 

Final String:   Javascript is a popular language 

Javascript string remove all line breaks using split() and join()

The split() method in javascript returns an array of substrings formed by splitting a given string.

The join() method in javascript joins the elements of the array back into a string.

Example:-

Remove all line breaks from the string “\n Javascript \nis a popular \nlanguage \n”

The below code shows that we split the original string based on line break (\n) using the split() method into an array of substrings. The array formed will have the elements {javascript, is, a, popular, language}. Then join the array back into one string using the join() method.

Code:-

let dummyString = " \n Javascript\n is a popular \nlanguage \n"
let stringWithoutLineBreaks = dummyString.split('\n').join('')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringWithoutLineBreaks)

Output:-

Original String:  
 Javascript
 is a popular 
language 

Final String:   Javascript is a popular language 

Javascript string remove line breaks from start and end of the string

Javascript’s trim() method removes all the white space characters from the start and end. These whitespace characters include space, tab, line breaks, etc.

Example:-

Remove the line breaks only from the start and end of the string “\n Javascript \nis a popular \nlanguage \n”

Here, in the below code, we are using the trim() method to remove the line breaks from the start and end of the string.

Code:-

let dummyString = " \n Javascript \nis a popular \nlanguage \n"
let finalString = dummyString.trim()
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String:  
 Javascript 
is a popular 
language 

Final String: Javascript 
is a popular 
language

Read More:

I hope this article helped you to delete line breaks from a string in javascript. Good Luck !!!