If the last character is a line break

(Javascript) How do i detect if the last character is a line break? For example, this would return false:

Thanks for deleting my minecraft world.
Prepare to die.

But this would return true:

 Hello, i am a minecraft player.
‎

I guess you could check if the last line is equal to " "

But what if the last line is blank?

Then check if the last line is “”

Just a hint: the special characters you are looking for here are CR and LF which are represented in JavaScript by '\r' and '\n' respectively.

Here are your examples in Notepad++ with “Show all characters” switched on:

image

Without the CRLF at the end of Line 2, there is no Line 3.

Whereas in example 2:

image

The CRLF creates another line.

This StackOverflow answer might help, but the guy has confused the order of \r\n which makes the answer a little harder to understand: https://stackoverflow.com/a/52479035

But basically, I think you could check for the '\r\n' string using .indexOf.

Also, on some non-windows systems, a line break can be represented by just \n on its own. To further confuse you: Newline - Wikipedia

Have fun!

3 Likes