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

  • SEARCH
  • Home
  • 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 7415321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:16:47+00:00 2026-05-29T07:16:47+00:00

How would I split a javascript string such as foo\nbar\nbaz to an array of

  • 0

How would I split a javascript string such as foo\nbar\nbaz to an array of lines, while preserving the newlines? I’d like to get ['foo\n', 'bar\n', 'baz'] as output;

I’m aware there are numerous possible answers – I’m just curious to find a stylish one.

With perl I’d use a zero-width lookbehind assertion: split /(?<=\n)/, but they are not supported in javascript regexs.

PS. Extra points for handling different line endings (at least \r\n) and handling the missing last newline (as in my example).

  • 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-29T07:16:48+00:00Added an answer on May 29, 2026 at 7:16 am

    You can perform a global match with this pattern: /[^\n]+(?:\r?\n|$)/g

    It matches any non-newline character then matches an optional \r followed by \n, or the end of the string.

    var input = "foo\r\n\nbar\nbaz";
    var result = input.match(/[^\n]+(?:\r?\n|$)/g);
    

    Result: ["foo\r\n", "bar\n", "baz"]

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

Sidebar

Related Questions

I would like to sort an array of strings (in JavaScript) such that groups
I would like to split the example string: ~Peter~Lois~Chris~Meg~Stewie on the character ~ and
I am writing many files to disk, and I would like to split them
What regex pattern would need I to pass to java.lang.String.split() to split a String
I have a large source repository split across multiple projects. I would like to
I want to split certain text using JavaScript. The text looks like: 9:30 pm
Could someone help me to create a JavaScript function that would turn the string
I have limited JS experience, and I need to split a string that would
In Javascript, I am getting a string like I have a string exactly like
I want to split following string into two parts using split function of javascript

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.