replace *except* where...

R

Robert Mark Bram

Hi All,

I have the following to replace newline chars with <br> in a string:

..replace(/\n/g,"<br>")

How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?

Thanks for any advice!

Rob
:)
 
E

Evertjan.

Robert Mark Bram wrote on 27 sep 2004 in comp.lang.javascript:
I have the following to replace newline chars with <br> in a string:

.replace(/\n/g,"<br>")

How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?

if(/#$#$#$#/.test(t))alert("error")

t=t.replace(/\n/g,"#$#$#$#")
..replace(/#$#$#$#<p>/g,"\n<p>")
..replace(/<br>#$#$#$#/g,"<br>\n")
..replace(/#$#$#$#/g,"<br>")

// all replaces on one line please !!

not tested
 
R

Robert Mark Bram

Hello Evertjan!
if(/#$#$#$#/.test(t))alert("error")

t=t.replace(/\n/g,"#$#$#$#")
.replace(/#$#$#$#<p>/g,"\n<p>")
.replace(/<br>#$#$#$#/g,"<br>\n")
.replace(/#$#$#$#/g,"<br>")

// all replaces on one line please !!

#$#$#$# seemed to cause a few problems. Instead of #$#$#$# I used jxjxjxjx
and it works well - thank you very much!

Rob
:)
 
G

Grant Wagner

Robert said:
Hi All,

I have the following to replace newline chars with <br> in a string:

.replace(/\n/g,"<br>")

How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?

Although the end tag for a paragraph is listed as optional <url:
http://www.w3.org/TR/REC-html40/struct/text.html#edef-P />, you should get
used to coding a paragraph as:

<p>Paragraph text</p>

rather than:

Paragraph text<p>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top