Home > Software design >  Is it safe to just put "\n" instead of "[\r\n]" when writing a JavaScript Reg
Is it safe to just put "\n" instead of "[\r\n]" when writing a JavaScript Reg

Time:07-29

The common method suggested by numerous StackOverflow answers and articles across the internet to match a line break in regular expressions is to use the ubiquitous token [\r\n]. It is supposedly to ensure compatibility with Windows systems, since Windows uses the carriage return \r and line feed \n together to make a new line, as opposed to just the line feed \n for UNIX based operating system such as Linux or Mac.

I'm beginning to think JavaScript ignores this distinction and just treats every line break as \n.

Today, I did an experiment where I created a text file with 10 carriage returns, opened up the text file, then copy/pasted the carriage returns into the regular expression tester at https://regex101.com.

When I tested all those carriage returns against the simple regular expression \r, nothing matched. However, using the alternative \n matched all 10 carriage returns.

So my question is, based on my experiment, is it safe to just write \n instead of [\r\n] when matching line breaks in JavaScript?

CodePudding user response:

No, do not replace [\r\n] with \n.

Line ends at http://regex101.com are only \n and that is why you had no match with \r.

In real texts, both carriage return and line feed characters might need matching.

Besides, the dot does not match \r in JavaScript regex.

CodePudding user response:

JavaScript treats newlines as \n, that's why it matched all when you tested it. \r\n is windows style of representing new lines while Unix based systems uses \n. If you are not sure, you can use this regex: /\r?\n/

  • Related