Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl Code

Backslash doesn't help too much!

         

jimijoe

8:15 pm on May 11, 2004 (gmt 0)

10+ Year Member



Hello,

With refference to: [webmasterworld.com...]

adding backslash doesn't help too much!

(/\r\n/g); adding backslash (/\\r\\n/g); comes to (/\n/g);

backslash saved \n but not \r

So, how can I fix it?
Some script use backslash ( \ ), how can I keep it there?

Thanks

upside

3:54 am on May 12, 2004 (gmt 0)

10+ Year Member



What is the context that (/\r\n/g); is in? Post the entire line.

jimijoe

7:22 am on May 12, 2004 (gmt 0)

10+ Year Member



This is the entir line:
contents = contents.replace(/\r\n/g, '<br>');

upside

11:29 pm on May 12, 2004 (gmt 0)

10+ Year Member



That is a regex in JavaScript. This is comparable statement in Perl:

$contents =~ s#(\r¦\n)#<br>#g;

Make sure to fix the ¦ pipe for your system

timster

3:12 am on May 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is comparable statement in Perl:
$contents =~ s#(\r¦\n)#<br>#g;

I don't think so. It looks to me that the poster's regex is meant to match a Windows CRLF line ending. Some folks write that at \r\n but the peril there is that different platforms interpret \r and \n differently.

I find it more foolproof to use the octal ASCII characters, like so:

s/\015\012/<br>/gs;

Still, that makes for ugly HTML, so why not leave the newlines in after adding the <br>'s? And while you're at it, why not translate with Unix & Mac line endings?

s/((\015(\012)?)¦(\012))/<br>\015\012/gs;