Skip to content

Commit

Permalink
Tipify normalize-line-endings
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed May 27, 2024
1 parent b884322 commit e28724c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions content/snippets/js/s/normalize-line-endings.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
---
title: Normalize line endings
type: snippet
title: Normalize line endings in a JavaScript string
shortTitle: Normalize line endings
type: tip
language: javascript
tags: [string,regexp]
cover: red-light
dateModified: 2020-10-21
excerpt: Having trouble with inconsistent line endings in your strings? Normalize them with this handy JavaScript function!
dateModified: 2024-05-24
---

Normalizes line endings in a string.
Historically, different operating systems have used **different characters to represent line endings** in text files. For example, **Windows** uses `'\r\n'` (carriage return + line feed), **Unix-like systems** use `'\n'` (line feed), and **older Mac systems** used `'\r'` (carriage return). All of these differences can cause issues when working with text files or strings.

- Use `String.prototype.replace()` and a regular expression to match and replace line endings with the `normalized` version.
- Omit the second argument, `normalized`, to use the default value of `'\r\n'`.
**Normalization** is the process of converting all line endings in a string to a consistent format. Often, you'll want to convert all line endings to a specific format, such as `'\r\n'` or `'\n'`.

Luckily, all you need to do is use `String.prototype.replace()` with a **regular expression** to match and replace line endings. As the sequences of characters are known and ordered consistently, you need only check for an **optional carriage return** (`\r?`) **followed by a line feed** (`\n`).

```js
const normalizeLineEndings = (str, normalized = '\r\n') =>
const normalizeLineEndings = (str, normalized = '\n') =>
str.replace(/\r?\n/g, normalized);

normalizeLineEndings('This\r\nis a\nmultiline\nstring.\r\n');
// 'This\r\nis a\r\nmultiline\r\nstring.\r\n'
normalizeLineEndings('This\r\nis a\nmultiline\nstring.\r\n', '\n');
// 'This\nis a\nmultiline\nstring.\n'

normalizeLineEndings('This\r\nis a\nmultiline\nstring.\r\n', '\r\n');
// 'This\r\nis a\r\nmultiline\r\nstring.\r\n'
```

> [!NOTE]
>
> As the much older Mac systems are no longer in common use, you can safely **ignore the standalone carriage return** (`\r`) character in most cases. However, if you need to support these systems, you can modify the regular expression to include it: `/\r?\n|\r/g`.

0 comments on commit e28724c

Please sign in to comment.