Skip to main content

How to Remove Line Breaks from a String in JavaScript

When working with text from different sources—like user input from a <textarea> or data from a file—you often need to standardize its formatting. A common task is to remove line break characters (\n or \r\n) to convert multi-line text into a single line.

This guide will teach you the most effective methods for removing line breaks using regular expressions with the replace() and replaceAll() methods. You will learn how to remove all line breaks, how to trim them from only the start and end of a string, and how to normalize all whitespace into single spaces.

The Core Problem: Different Line Break Characters

Different operating systems use different characters to represent a line break:

  • Unix/Linux/macOS: \n (Line Feed or LF)
  • Windows: \r\n (Carriage Return + Line Feed or CRLF)

A robust solution must account for both \r and \n characters.

Removing All Line Breaks from a String

The most reliable way to remove all line breaks from anywhere in a string is to use a regular expression with the replace() or replaceAll() method.

For example, you have a string with line breaks mixed in and want to collapse it into a single line.

// Problem: How to remove all \n and \r characters?
const multiLineString = 'This is the first line.\r\nAnd this is the second.\nThis is the third.';

The solution uses a regular expression that matches any newline character (\r or \n) globally.

const multiLineString = 'This is the first line.\r\nAnd this is the second.\nThis is the third.';

// The regex /[\r\n]/g matches either \r or \n, globally (g flag).
const singleLineString = multiLineString.replace(/[\r\n]/g, '');

console.log(singleLineString);
// Output: 'This is the first line.And this is the second.This is the third.'

Output:

This is the first line.And this is the second.This is the third.
note

In modern JavaScript, replaceAll() can be more readable for this, but replace() with a global regex is the classic, universally-compatible approach.

How it works:

  • [\r\n]: This is a character set. It tells the regex engine to match any single character that is either a \r or a \n.
  • /g: This is the global flag. It's crucial. Without it, replace() would only remove the very first line break it finds. The global flag ensures it replaces all occurrences.

Trimming Line Breaks from the Start and End

If you only want to remove line breaks (and other whitespace) from the beginning and end of a string, but preserve those in the middle, the String.prototype.trim() method is the perfect tool.

For example, your string has unwanted leading and trailing newlines.

// Problem: How to clean up only the ends of the string?
const messyString = '\n\n Hello\nWorld \r\n';

Solution:

const messyString = '\n\n   Hello\nWorld   \r\n';

const trimmedString = messyString.trim();

console.log(trimmedString);
// Output: 'Hello\nWorld'

Output:

Hello
World
note

The trim() method conveniently removes all types of whitespace—including spaces, tabs, and all line break characters—from both ends of the string.

Normalizing All Whitespace (Line Breaks and Spaces)

A more advanced task is to "normalize" a string. This typically means replacing all line breaks with spaces and collapsing any sequences of multiple spaces into a single space, resulting in a clean, single-line sentence.

For example, the string has a messy combination of line breaks and inconsistent spacing.

// Problem: How to convert this into a clean, single-spaced line?
const messyString = ' First part. \n\n Second part. \r\n Third part. ';

The solution uses a regular expression that matches any whitespace character (\s) one or more times (+) globally.

const messyString = '  First part.  \n\n  Second part. \r\n  Third part. ';

// 1. Replace one or more whitespace characters with a single space.
// 2. Trim any leading/trailing space from the final result.
const normalizedString = messyString.replace(/\s+/g, ' ').trim();

console.log(normalizedString);
// Output: 'First part. Second part. Third part.'

Output:

First part. Second part. Third part.

How it works:

  • \s: This is a special regex character that matches any whitespace, including spaces, tabs, and line breaks (\r, \n).
  • +: This is a quantifier that means "one or more times." So, \s+ matches any continuous block of whitespace.
  • /g: The global flag ensures all blocks of whitespace are replaced.

Conclusion

JavaScript's string methods and regular expressions provide powerful and concise tools for managing line breaks.

  • To remove all line breaks from a string, use string.replace(/[\r\n]/g, '').
  • To remove line breaks and other whitespace from only the start and end, use the simpler string.trim() method.
  • To convert all line breaks to spaces and collapse multiple spaces, the best method is string.replace(/\s+/g, ' ').trim().

By choosing the right tool for your specific goal, you can easily clean and normalize any text data.