JavaScript \n Metacharacter | RegEx Match New Line

The JavaScript \n metacharacter is used to match new line in specified string using JavaScript regular expression. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="xyz"></p>

   <script>
      let myString = "JavaScript is Fun.\nIs not it?";
      let pattern = /\n/;
      document.getElementById("xyz").innerHTML = myString.search(pattern);
   </script>
   
</body>
</html>
Output

Since the new line character, that is \n is available at index number 18 in the specified string, therefore the output produced by above JavaScript code is 18.

Note: The search() method is used to search a substring (value) in a string using regular expression.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋