Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to have my RegExp match anything but a newline

\r?\n
like image 962
Jiew Meng Avatar asked Dec 03 '10 10:12

Jiew Meng


People also ask

What is \r and \n in regex?

Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.

Does regex match anything?

In regular expressions, we can match any character using period "." character. To match multiple characters or a given set of characters, we should use character classes.

What is the use of \n in regex?

"\n" matches a newline character.

Does empty regex match everything?

An empty regular expression matches everything.


1 Answers

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.

like image 140
Gumbo Avatar answered Sep 17 '22 13:09

Gumbo