JavaScript - How to Replace Line Breaks With 'br' Tag? Last Updated : 06 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 D devi_johns Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-RegExp JavaScript-Questions +2 More Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis 8 min read Like