Javascript RegExp: match newlines (\ r? \ N)

I want my RegExp parameter to be anything but a new line

\r?\n 
+6
javascript regex
source share
3 answers

This should do it:

 /(?:[^\r\n]|\r(?!\n))/g 

This matches any character except \r and \n , or one \r followed by \n .

+7
source share

you can use a negative delimiter!

so (?!(\r|\n))

try

or is it possible

 .+?(?!(\r|\n)) 
0
source share

As an alternative suggestion, why not avoid regexp?

 var newText = oldText.replace("\r","").replace("\n",""); 

This will return a string deleting all instances of \r and \n

0
source share

All Articles