Find a new line character with JavaScript RegExp.


The new line character, we denote as
.
This is used to make a line break. ASCII code for
is 10 and it is also called Line Feed (LF). Now, let's see how to find a new line character using RegExp.

A RegExp is an object that specifies the pattern used to do search and replace operations on the string or for input validation. RegExp was introduced in ES1 and it is fully supported by all browsers

The RegExp
meta character finds an index value of the first occurrence of a newline character in a given text.

Syntax

Following is the syntax for new line character −

new RegExp("\
") or simply : /
/

/
/,
is introduced in ES1. It is fully supported by all browsers. Like, Chrome, IE, Safari, Opera, FireFox and Edge.

Algorithm

  • STEP 1 − Define a string having at least a new line character.
  • STEP 2 − Define the RegExp pattern for new line character
  • STEP 3 − Apply search(pattern) on the above-defined string to find the index of the new line character.
  • STEP 4 − Display the result, the index of the new line character.

Notice that using the above approach, we will find the index of the new line character in the given string.

Example 1

In the program below, we use string search(pattern) to find a new line character (
) in the given string. We use RegExp as /
/. The string search() method returns the index of the matched character. So it returns the index of the new line character.

<!DOCTYPE html> <html> <body> <h2>Finding a new line character</h2> <p>The position of newline character is : <span id= "result"></span> </p> <script> let text = "Hello, Devika.
Welcome back."
; let pattern = /
/
; let result = text.search(pattern); document.getElementById("result").innerHTML = result; </script> </body> </html>

If a new line character exists in the given text, the search() method will return the index of the newline character else it will have a -1.

Example 2

In the program below, we take a string with no newline characters and try to find a form feed character in the string. See what our output looks like.

<!DOCTYPE html> <html> <body> <h2>RegExp
finding</h2> <p id = "result"></p> <script> const text = "Hello, Devika. Welcome back"; const regExp = /
/
; const output = text.search(regExp); if(output == -1) { document.getElementById("result").innerHTML = "No new line character in the text. "; } else { document.getElementById("result").innerHTML = "Index of a new line character: " + output; } </script> </body> </html>

Here, the text does not have a newline character. So, the if statement will execute and print No newline character in the text. If text has
, then else will execute and return the index of the
.

Example 3

Finding and Replacing new line character

We can replace newline characters based on requirements. We can see how to do it. In the program below, we find the new line character and replace it with the space using the split() and join()methods.

<!DOCTYPE html> <html> <body> <h1>Replace new line character</h1> <p>After replacing new line character : <span id= "result"></span> </p> <script> let text = "Hello, Devika.
Welcome back."
; let result = text.split("
"
).join(" "); document.getElementById("result").innerHTML = result; </script> </body> </html>

Here, we have used split() and join() methods to replace new line characters from a given text. We can do also in simple way like

Example 4

In the program below, we find the new line character and replace it with the space using the String replace() method.

<!DOCTYPE html> <html> <body> <h1>Replace new line character</h1> <p>After replacing the new line character : <span id= "result"></span> </p> <script> let text = "Hello, Devika.
Welcome back."
; let result = text.replace("
/g"
, " "); document.getElementById("result").innerHTML = result; </script> </body> </html>

We can do replacements in other ways also. I have mentioned simpler ways to do replacements. "g" in the code above is for global matches i.e. finds all the occurrences instead of stopping at the first position.

In RegExp we modifiers like g, i, m. i for performing case-insensitive matching and m for performing multiline matching.

We have seen how to find the new line character using RegExp in JavaScript. Hope this gave some clarification to find a new line character.

Updated on: 26-Aug-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements