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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:08:07+00:00 2026-05-20T02:08:07+00:00

I have some paragraphs like this: This is the first para. r\r\n\n This is

  • 0

I have some paragraphs like this:

“This is the first para. r\r\n\n This
is the second one with lot of new line
after \n\n\n\n\n\n And the last para.
\n\r\r”

I want to remove the new lines and wrap each paragraph with the <p> tag. I’m expecting output as follows:

<p>This is the first para.</p>
<p>This is the second one with lot of new line after</p>
<p>And the last para.</p>
  • 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-20T02:08:08+00:00Added an answer on May 20, 2026 at 2:08 am
    var d = "line 1\n\nline2\n\n\n\nline3";
    $('body').append('<p>'+d.replace(/[\r\n]+(?=[^\r\n])/g,'</p><p>')+'</p>');
    

    something like this perhaps?

    If you find the line contains any new lines/carriage returns at the beginning or end, remember to call a .trim() on the string first before replacing the values (using this example, d.trim().replace(...))

    More robust solution

    function p(t){
        t = t.trim();
        return (t.length>0?'<p>'+t.replace(/[\r\n]+/,'</p><p>')+'</p>':null);
    }
    document.write(p('this is a paragraph\r\nThis is another paragraph'));
    

    PHP Version:

    $d = "paragraph 1\r\nparagraph 2\r\n\r\n\r\nparagraph3";
    echo "<p>".preg_replace('/[\r\n]+/','</p><p>',$d)."</p>";
    

    http://www.ideone.com/1TRhh

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

Sidebar

Related Questions

I have some text, something like this: Paragraphs of text (SOME KNOWN TEXT)Unknown Text(SOME
I have some XML which contains records and sub records, like this: <data> <record
I have some content like this <p>some content, paragraph 1</p> <p>some content, paragraph 2</p>
I have some xml like this: <animal name=Bow Wow type=dog> <birthDate>May 17,2001</birthDate> <descirption>Bla bla
WordPress spits posts in this format: <h2>Some header</h> <p>First paragraph of the post</p> <p>Second
For a blog like project, I want to get the first few paragraphs, headers,
Let's say we have a box with some short paragraphs: <div style=overflow:hidden> <p>Some text</p>
I have a paragraph of text and i want to replace some words using
<p title=The test paragraph>This is a sample of some <b>HTML you might<br>have</b> in your
I have an email template where the user can enter text like this: Hello

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.