In this article, we are given a multiline string and the task is to replace line breaks with <br> tag.
Input: `Geeks for Geeks is a computer science portal where people study computer science` Output: "Geeks for Geeks is <br> a computer science portal <br> where people study computer science"
To achieve this we have the following methods :
Method 1: Using Regex, in this example, we are creating a paragraph and a button and on clicking the button we are changing (Adding the <br>)the text of the paragraph.
Example 1: This example shows the use of string.replace() method to replace the new line with <br>.
Method 2: Using split() and join(), in this method, we split the string from the delimiter “\n” which returns an array of substrings, and then we join the Array using the join method and pass <br /> so that every joining contains <br />.