Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:31:57+00:00 2026-05-23T19:31:57+00:00

How do I replace multiple \n ‘s with just one? So if a user

  • 0

How do I replace multiple \n‘s with just one? So if a user enters

blah 

blahdy blah



blah blah

I want it to end up looking like.

blah
blahdy blah
blah blah

I know I could loop through with a while() but would rather use a regular expression, since I believe it’s more efficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:31:58+00:00Added an answer on May 23, 2026 at 7:31 pm

    This worked for me:

    string = string.replace(/\n+/g, '\n');
    

    As others have said, it replaces each occurrence of one or more consecutive newline characters (\n+) with just one.

    The g effects a “global” replace, meaning it replaces all matches in the string rather than just the first one.

    Edit: If you want to take into account other operating systems’ line ending styles as well (e.g., \r\n), you could do a multi-step replace:

    string = string.replace(/(\r\n)+/g, '\r\n') // for Windows
        .replace(/\r+/g, '\r')                  // for Mac OS 9 and prior
        .replace(/\n+/g, '\n');                 // for everything else
    

    OR (thanks to Renesis for this idea):

    string = string.replace(/(\r\n|\r|\n)+/g, '$1');
    

    If you know in advance what sort of text you’re dealing with, the above is probably overkill as it carries am obvious performance cost.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I replace multiple consecutive newlines with one newline. There could be up
I want to replace multiple newline characters with one newline character, and multiple spaces
I'm trying to replace multiple sequential <br> tags with just one <br> tag using
i'm just wondering how I can replace multiple instances of - with just one
It seems like Traits could completely replace interfaces, abstract base classes, mixins, and multiple
How can I replace multiple spaces in a string with only one space in
I am looking for a tool to replace multiple lines through out a project.
I want to replace multiple elements in my string in Objective-C. In PHP you
I'd like to replace a multiple select box like: <select multiple=multiple name=options> <option value=option1>option1</option>
I'm looking for a decent tool that can do search and replace over multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.