-2

Update: my original test involving copy/pasting from a text file into the browser was flawed. I created a new test in JavaScript which verified that carriage return \r is in fact being matched.

The following code logs ['\r', '\r', '\r'] to the console, which verifies that the \r is being matched:

<script>
    const CarriageReturn = String.fromCharCode(13); // char code for carriage return is 13
    const str = CarriageReturn + CarriageReturn + CarriageReturn;
    const matches = str.match(/\r/g);
    console.log(matches); // this will output ['\r', '\r', '\r']
</script>

Original Question

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?

user3163495
  • 2,425
  • 2
  • 26
  • 43
  • 1
    depends where you get the data. I bet the browser converted \r\n to \n when pasting. – user253751 Jul 28 '22 at 21:26
  • @WiktorStribiżew user253751's comment turned out to be the reason why the carriage returns (`\r`) weren't being matched. I posted an answer but can't accept it until tomorrow. – user3163495 Jul 29 '22 at 16:00

3 Answers3

2

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.

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
2

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/

Marvellous
  • 52
  • 2
-1

After doing a different test, it appears JavaScript does make a distinction between \r and \n, but not in all cases. Here are the exceptions:

  1. If you generate a carriage return string in JavaScript using String.fromCharCode(13), and try to match it with pattern \r, the pattern will match successfully.
  2. If you type a line break with your keyboard directly into a <textarea> in your browser, it is interpreted by JavaScript as just \n. There will be no matches for \r.
  3. If you copy/paste text containing carriage returns (\r) from a text file into a <textarea> in your browser, your browser will convert all the sequences of \r\n into just \n. So, it will appear as if JavaScript is ignoring the \rs in your text, but it's only because your browser removed them in the process of pasting it into the <textarea>.

I updated my original question with the test I ran to confirm that the \r token is matched when generated with String.fromCharCode(13).

user3163495
  • 2,425
  • 2
  • 26
  • 43