user6074580 user6074580 - 1 year ago 49
Javascript Question

Regular expression not supporting line skipping \n

I'm trying to find the right regular expression for a number with a line skip at the end (

) but every time it's not working.

My regular expression is

EDIT : The text area contains :


Here's my code ( I'm trying to validate what's in the textarea and it's only numbers going there with
at the end of each lines ) :

function valideChamp()
var rExp1 = /^\d+(\n)$/;
var aChamps = document.querySelector("textarea").value;

if (rExp1.test(aChamps.value)==true){
else {
return false;

Answer Source

If you want to check for any line containing only a number on it, you can use:


If you just want to check that there's only a number, and then a newline, and nothing else:


(which is what you were checking for, but that's an odd input pattern.)

If you want to make sure textarea ONLY has lines that are numbers, it might be simpler to check that string.replace(/[0-9\r\n]/g, '') == ''. This will confirm if it contains only numbers and newlines.