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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:05:52+00:00 2026-05-29T23:05:52+00:00

My need is to strip embedded line breaks but not paragraph breaks when the

  • 0

My need is to strip embedded line breaks but not paragraph breaks when the browser is not IE.

The reason for this need is to nicely format alert messages. Turns out that the major non-IE browsers format the messages nicely by themselves, inserting line breaks in a nice place. However, IE will not do this. So a non-formatted line simply expands the width of the alert box. My solution is to format all alert messages with line breaks for IE, and strip out the line breaks when the browser is not IE. This gives a nicely formatted alert message regardless of the browser. If this extra processing is not done, then you either have ugly wide alert boxes in IE (without any breaks) or ragged formated text in the non-IE browsers as the hard-coded line breaks and browser-added line breaks are both present.

A further complication is that I don’t want to strip ALL line breaks, because doing so would also remove paragraph breaks. So the final requirement is to strip all single line breaks but not “paragraph” breaks, which are simply two consecutive line breaks.

Yet a further complication is that the strings are being processed in PHP, which requires backslashes to be escaped. So rather than considering line breaks as \n, they are \\n, and the regex has to look for the \\n pattern.

Also please note: the majority of our important “popup messages and dialogs” are done using jquery to provide a nicely formatted display. So another approach would be to do away with alerts entirely and replace them with jquery dialogs. However, in these cases we do not desire to go that far, and simply want a nicely formatted alert box.

We normally do the regex processing in PHP, which works fine. The problem is that I would also like to have the equivalent processing available in pure javascript if this is possible. I am suspecting that the negative lookahead processing might not be supported in javascript, at least in the way it is in PHP. If so, that raises the difficulty. So far I have been unable to come up with a javascript version.

Here is the PHP code that achieves the intended result:

/**
 * Removes embedded line breaks but not paragraph breaks if not IE
 *
 * @param string $message - formatted for IE, with embedded paragraph and line breaks
 * $is_IE - set elsewhere to indicate if the browser is IE
 */
function FormatAlert($message)
{
    global $is_IE;
    return ($is_IE) ?  $message : preg_replace('/(?<!\\\n)\\\n(?!\\\n)/', ' ', $message);
}

Thus the need is to create an equivalent regex for javascript.

In further thinking, it occurs to me that if I can come up with a single regex then the workaround would be to temporarily replace double line breaks used for paragraph breaks with a special symbol, then replace the remaining single line breaks, and then finally replace the special symbol with the double line break.

  • 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-29T23:05:55+00:00Added an answer on May 29, 2026 at 11:05 pm

    Javascript supports negative lookahead, but not lookbehind.

    Originally I answered you might not need lookbehind with: str.replace(/\n(?!\n)/g, ''); which will remove all newlines not followed by another. This might work fine for your case of an alert popup, but it will actually remove the last newline in a double-newline sequence (since it is not followed by a newline) turning a double-lined paragraph break into a simple newline. To fix this you will need to use another replace to put back the other newlines by replacing any left with an extra that was stripped: http://jsfiddle.net/rgthree/99jCd/

    var str = "This is an oddly\nbroken paragraph that\nwill turn"+
              "into one line.\n\nThis will still be a new\nand separate"+
              "paragraph that\nis also not broken.";    
    str = str.replace(/\n(?!\n)/g, ' ').replace(/\n+\s/g, '\n\n');​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im not proficient in regex at all, but I need to strip IDs from
I need to strip leading spaces from a column in Oracle. I've Googled but
I need to strip all xml tags from an xml document, but keep the
I need to strip the last line from a text file. I know how
I need to strip the label off the front of strings, e.g. note: this
I need to strip this string <a class=BC_ANCHOR href=http://www.msn.com onClick=something target=_blank>MSN</a> into <a href=http://www.msn.com>MSN</a>
I need to strip link tags from a body of text but keep the
I need to strip out any movies that are not hosted by YouTube from
I am struggling to find this - I need to strip all empty lines
I need to strip out any &id=SomeValue from a Url.PathAndQuery. Where SomeValue could be

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.