Jiew Meng
Jiew Meng

Reputation: 88207

Javascript RegExp: Match anything but newlines (\r?\n)

I want to have my RegExp match anything but a newline

\r?\n

Upvotes: 10

Views: 11651

Answers (3)

Gumbo
Gumbo

Reputation: 655269

This should do it:

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

This matches either any character except \r and \n or a single \r that is not followed by \n.

Upvotes: 9

Breezer
Breezer

Reputation: 10490

you can use the negative delimiter !

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

try that

or this maybe

.+?(?!(\r|\n))

Upvotes: 0

Tom Gullen
Tom Gullen

Reputation: 61727

As an alternative suggestion why not avoid regexp?

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

This will return the string removing all the instances of \r and \n

Upvotes: 0

Related Questions