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 8877847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:34:03+00:00 2026-06-14T19:34:03+00:00

I am trying to find and replace line breaks in text using javascript. The

  • 0

I am trying to find and replace line breaks in text using javascript.

The following works. It replaces the character a with the character z.

var text = "aasdfasdf";
text= text.replace(/a/g,"z");
alert(text);

The following based on other posts on this and other message boards does not. Basically the javascript does not fire:

var text = "aasdfasdf";
text= text.replace(/\n/g,"z");
alert(text);

…Here is one of many posts that suggests it should work.

JavaScript: How to add line breaks to an HTML textarea?

and by the way following does not work for me in Firefox either:

text = text.replace(/\n\r?/g, '<br />'); or
text = text.replace("\n", '<br />'); 

Note: I realize there are no line breaks in the text variable..I am just using a simple string for testing purposes.

Can anyone see what could be wrong or show me a way to do this that actually works.

Thanks.

  • 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-06-14T19:34:05+00:00Added an answer on June 14, 2026 at 7:34 pm

    I’d cover my bets by handling \r\n (the sequence), and then handling \r and \n through a character class, like this:

    text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');
    

    The first replace turns the sequence \r\n into <br />. The second replace replaces any \r or \n characters found on their own with the string.

    More on regular expressions in JavaScript here.

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

Sidebar

Related Questions

I am trying to find and replace html data using jquery. The jquery script
I'm trying to find every instance of @username in comment text and replace it
I am trying to use a macro to find and replace some text in
I'm trying to display the contents of a textarea, including any line breaks. var
I'm trying to find a block of text and replace it with a modified
I am trying to do a simple find/replace on all text files in a
Possible Duplicate: Replace first line of a text file in Java Java - Find
I am trying to find and replace a couple of words in a file
I'm trying to find a way to replace spaces and double quotes with pipes
I'm trying to find all href links on a webpage and replace the link

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.