Replace All Line Breaks with
in JavaScript

JavaScript Program to Replace All Line Breaks with <br>

In this example, you will learn to write JavaScript program that will replace all line breaks in a string with the <br> tag.

To understand this example, you should have the knowledge of the following JavaScript programming topics:


JavaScript String

JavaScript String replace()

JavaScript String split()

JavaScript Array join()


Example 1: Replace All Line Breaks Using RegEx


In the above example:

The RegEx is used with the replace() method to replace all the line breaks in string with <br>.

The pattern /(\r\n|\r|\n)/ checks for line breaks.

The pattern /g checks across all the string occurrences.


Example 2: Replace All Line Breaks Using Built-in Methods

In the above example, the built-in methods are used to replace all line breaks with <br>.

Post a Comment

Previous Post Next Post