JavaScript RegExp n Metacharacter

The regular expression "\n" in JavaScript identifies the presence of a new line character within a given input string. When the new line character is found, the method returns the index position of the match; otherwise, it returns "-1" indicating no match found. This Metacharacter can be employed for testing, searching, and matching specific strings as needed.

Syntax

The syntax is utilized to indicate the newline characters within the input string.

Example

/\n/
or
new RegExp("\n")

Description:

  • When utilizing the new line metacharacter, no modifier is required for it to function with all values.
  • Employing the regex method allows us to work with regex patterns and metacharacters effectively.
  • Supported Browsers

The RegExp \n Metacharacter supports the following browsers supported using the javascript function:

  • Google Chrome
  • Apple Safari
  • Mozilla Firefox
  • Opera
  • Internet Explorer
  • Examples

In the upcoming illustrations, we will demonstrate how to utilize the "\n" regex metacharacter in JavaScript for searching, matching, testing, and executing newline values.

Example 1

The example below demonstrates the use of the newline metacharacter in regular expressions in JavaScript. When a value is present, the test method will return true; otherwise, it will return false.

Example

<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \n (new line) Metacharacter
</title>
<style>
#demo1{
background-color: orange;
border: 1px solid black;
width: 350px;
}
</style>
</head>
<body>
<div id = "demo1">
<h3>
JavaScript RegExp \n Metacharacter
</h3>
<h4> We can use the validation for the new line values position in the input string.
</h4>
<div id = "regex_information">
</div>
<!-- click the button to get the new line position's output -->
<button onclick = "display();"> Click Here! </button>
</div>
<script>
function display() {
// use a new line regex pattern    
let input_pattern = /\n/;
// regex test method works with the input value and regex pattern
var t1 = input_pattern.test('\n992JavaScript2');
var t2 = input_pattern.test('Hii~.\n=@Err');
var t3 = input_pattern.test('%!@!#$%');
// output shows a boolean value for the availability of the new line
var ele = document.getElementById("regex_information");
ele.innerHTML = "\n992JavaScript2 : "+t1+"<br> Hii~.\n=@Err : "+t2+" <br> %!@!#$% : "+t3
}
</script>
</body>
</html>

Output

The output displays the special character within the string.

Example 2

In the provided illustration, the regex's newline metacharacter position in the string "tttttttttt" is demonstrated. If a newline character is not found, the output will display "-1".

Example

<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \n (new line) Metacharacter
</title>
<style>
#demo1{
background-color: orange;
border: 1px solid black;
width: 350px;
}
</style>
</head>
<body>
<div id = "demo1">
<h3>
JavaScript RegExp \n Metacharacter
</h3>
<h4> We can use the validation for the new line values position in the input string.
</h4>
<div id = "regex_information">
</div>
<!-- click the button to get the new line position's output -->
<button onclick = "display();"> Click Here! </button>
</div>
<script>
function display() {
//use new line metacharacter in regex
let input_pattern = /\n/;
var input_val1 = 'hello world\n';
var input_val2 = '!!#HiiErr';
var input_val3 = '\n%!@!#$%0';
// regex search method works with the input value and regex pattern
var t1 = input_val1.search(input_pattern);
var t2 = input_val2.search(input_pattern);
var t3 = input_val3.search(input_pattern);
//get output of the search method with new line regex pattern
var ele = document.getElementById("regex_information");
ele.innerHTML = "hello world\n : "+t1+"<br> !!#HiiErr : "+t2+" <br> \n%!@!#$% : "+t3;
}
</script>
</body>
</html>

Output

The result displays the index of the newline character within the string.

Example 3

The upcoming example illustrates the location of the newline metacharacter in JavaScript regular expressions. In cases where there is no newline value present, the output will be "null".

Example

<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \n (new line) Metacharacter
</title>
<style>
#demo1{
background-color: orange;
border: 1px solid black;
width: 350px;
}
</style>
</head>
<body>
<div id = "demo1">
<h3>
JavaScript RegExp \n Metacharacter
</h3>
<h4> We can use the validation for the new line values position in the input string.
</h4>
<div id = "regex_information">
</div>
<!-- click the button to get the new line position's output -->
<button onclick = "display();"> Click Here! </button>
</div>
<script>
function display() {
//use new line metacharacter in regex
let input_pattern = /\n/;
var input_val1 = 'hello world\n';
var input_val2 = '!!#HiiErr';
var input_val3 = '\n%!@!#$%0';
// regex match method works with the value and regex variable
var t1 = input_val1.match(input_pattern);
var t2 = input_val2.match(input_pattern);
var t3 = input_val3.match(input_pattern);
//output shows array format data on the web page
var ele = document.getElementById("regex_information");
ele.innerHTML = "hello world\n : "+t1+"<br> !!#HiiErr : "+t2+" <br> \n%!@!#$% : "+t3;
}
</script>
</body>
</html>

Output

The result displays the index of the newline character within the string.

Example 4

The provided illustration demonstrates the newline metacharacter that can be utilized in regular expressions in JavaScript. When a newline character is detected, it can be substituted with the specified input.

Example

<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \n (new line) Metacharacter
</title>
<style>
#demo1{
background-color: orange;
border: 1px solid black;
width: 350px;
}
</style>
</head>
<body>
<div id = "demo1">
<h3>
JavaScript RegExp \n Metacharacter
</h3>
<h4> We can use the validation for the new line values position in the input string.
</h4>
<div id = "regex_information">
</div>
<!-- click the button to get the new line regex's output -->
<button onclick = "display();"> Click Here! </button>
</div>
<script>
function display() {
//use new line metacharacter in regex
let input_pattern = /\n/;
var input_val1 = 'hello world\n';
var input_val2 = '!!#HiiErr';
var input_val3 = '\n%!@!#$%0';
// use replace the value
var replace_val = '@@';
// regex replace method works with the value and regex variable
var t1 = input_val1.replace(input_pattern, replace_val);
var t2 = input_val2.replace(input_pattern, replace_val);
var t3 = input_val3.replace(input_pattern, replace_val);
//output shows array format data on the web page
var ele = document.getElementById("regex_information");
ele.innerHTML = "hello world\n : "+t1+"<br> !!#HiiErr : "+t2+" <br> \n%!@!#$% : "+t3;
}
</script>
</body>
</html>

Output

The result displayed indicates the substituted value for the newline character within the string.

Conclusion

Regular expressions are utilized in JavaScript to identify line breaks within data.

The Metacharacter serves the purpose of validating data input by users.

Input Required

This code uses input(). Please provide values below: