JavaScript - How to Replace Line Breaks With 'br' Tag? Last Updated : 06 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different methods to replace line breaks with <br> tag in JavaScript.1. Using replace() with Regular ExpressionThe regular expression /\n/g matches all occurrences of line breaks in the string. The g flag ensures global replacement. JavaScript let s1 = "Hello\nWorld!\nWelcome to JavaScript."; let s2 = s1.replace(/\n/g, "<br>"); console.log(s2); OutputHello<br>World!<br>Welcome to JavaScript. The regular expression /\n/g matches all line breaks (\n) in the string.replace(/\n/g, "<br>") replaces every line break with the <br> tag.2. Using split() and join() Methodssplit() divides the string into an array of substrings at each line break, and join() concatenates them back together with <br> tags. JavaScript let s1 = "Line 1\nLine 2\nLine 3"; let s2 = s1.split("\n").join("<br>"); console.log(s2); OutputLine 1<br>Line 2<br>Line 3 split("\n") splits the string into an array where each element is a part of the string separated by line breaks.join("<br>") joins the array elements back together, inserting <br> tags between each line.3. Using replace() with \r\n for Windows Line BreaksIf you are dealing with line breaks from Windows-style text (\r\n), you need to adjust the regular expression to handle both \n and \r\n line breaks. JavaScript let s1 = "Line 1\r\nLine 2\nLine 3"; let s2 = s1.replace(/(\r\n|\n)/g, "<br>"); console.log(s2); OutputLine 1<br>Line 2<br>Line 3 The regular expression /(\r\n|\n)/g matches both Windows (\r\n) and Unix (\n) line breaks.replace(/(\r\n|\n)/g, "<br>") replaces both types of line breaks with <br> tags.4. Using split() and map() for Custom FormattingFor more advanced use cases where you might want to apply custom formatting while replacing line breaks, you can use split() to convert the string into an array and then use map() to process each part of the string before joining them with <br> tags. JavaScript let s1 = "Line 1\nLine 2\nLine 3"; let s2 = s1.split("\n").map(line => line.trim()).join("<br>"); console.log(s2); OutputLine 1<br>Line 2<br>Line 3 split("\n") divides the string at each line break.map(line => line.trim()) processes each substring to remove extra spaces (optional).join("<br>") reassembles the array with <br> tags in between.5. Using replace() with Multiple Line Break VariantsThis approach ensures that both line breaks (\n) and carriage returns (\r) are replaced with the <br> tag, especially when dealing with multiline text from different operating systems. JavaScript let s1 = "First Line\rSecond Line\nThird Line"; let s2 = s1.replace(/(\r\n|\r|\n)/g, "<br>"); console.log(s2); OutputFirst Line<br>Second Line<br>Third Line The regular expression /(\r\n|\r|\n)/g matches all types of line breaks (\r\n, \r, and \n).replace() then substitutes each match with a <br> tag. Comment More infoAdvertise with us Next Article JavaScript - How to Replace Line Breaks With 'br' Tag? devi_johns Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-RegExp JavaScript-Questions +2 More Similar Reads JavaScript - How to Remove All Line Breaks From a String? Here are different ways to remove all line breakes from a string in JavaScript.1. Using JavaScript RegEx with replace() Method This method uses regular expressions to detect and replace newlines in the string. It is fed into replace function along with a string to replace with, which in our case is 3 min read How to Create a New Line in JavaScript ? A new line can be made in JavaScript using the below-discussed methods.Using Escape Sequence `\n`The \n escape sequence represents a newline character in JavaScript strings and it can used to render a new line in JavaScript.JavaScriptconsole.log("GfG"); console.log("\n"); // New line console.log("Co 2 min read How to break JavaScript Code into several lines ? In this article, we will discuss how to break javascript code into several lines. We generally break the code into several lines so that it can be read easily by a third person or the programmer itself. There are a few ways to break JavaScript code into several lines: We can use the backslash \ char 1 min read How to Break Line Without using <br> Tag in HTML / CSS? Breaking lines in HTML without <br> tags can be achieved using block-level elements such as <div> or <p> combined with CSS properties like display: block; or display: inline-block; and others. These elements naturally wrap text or content, facilitating clean and structured layout d 2 min read How to create multi-line strings in JavaScript ? In JavaScript, the absence of native support for multi-line strings was a longstanding challenge. However, with the advent of ES6 (ECMAScript 2015), the things changed dramatically. ES6 introduced string literals, offering robust support for multi-line strings, which was a significant enhancement fo 3 min read How to add line break using <br> Tag in HTML? The <br> tag is used to create a line break in HTML. It allows you to break text into a new line without starting a new paragraph or block. This is helpful when you need to format text in a specific way, like in poems or addresses. For adding a line break, simply insert the <br> tag wher 1 min read How to add line breaks to an HTML textarea? Line breaks in an HTML textarea refer to the inclusion of newline characters (\n) that separate lines of text within the textarea. When users press Enter or Return, a line break is created, making text easier to read and format within the form element.The way of implementing line breaks differs from 2 min read How to Avoid a New Line with Tag? To avoid a new line with HTML tags, you can use inline elements instead of block elements. Inline elements, such as <span>, <a>, <img>, and <strong>, do not cause a line break after they are used, unlike block elements like <div>, <p>, or <h1>. This article 2 min read How to insert a line break in PHP string ? In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever '\n' is placed.Syntax:nl2br("string \n");where the string is the input string.Example 1: PHP Program to insert a line bre 2 min read Like