RegExp \n Metacharacter - Javascript RegExp

Javascript examples for RegExp:Metacharacter

Description

The \n character matches a newline character and returns the position where the newline character was found. If no match is found, it returns -1.

The following code shows how to Search for a newline character in a string:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from  w  w  w  . j  a v a 2  s . c  o m*/
    var str = "Visit java2s.com.\nLearn JavaScript.";
    var patt1 = /\n/;
    var result = str.search(patt1);
    document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

Related Tutorials