Jeffrey Simon
Jeffrey Simon

Reputation: 1034

Converting a regex with negative lookahead from a PHP format to a Javascript format

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.

Upvotes: 0

Views: 140

Answers (1)

rgthree
rgthree

Reputation: 7273

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');​

Upvotes: 1

Related Questions