Forum Moderators: open

Message Too Old, No Replies

java n00b, need help with string.replace

can't get it to replace <p>&nbsp;</p> with <br />

         

gilahacker

1:18 am on Apr 23, 2005 (gmt 0)

10+ Year Member



ok, I've tried this:
html = html.replace("<p>&nbsp;</p>","<br />");

(where 'html' is my string)

and this:

html = html.replace(/<p>&nbsp;<\/p>/g,"<br />");

and this:

html = html.replace(/<p>&nbsp;<\/p>/g,"<br \/>");

with no luck... what am I doing wrong?

BTW: I know html/php... but little Javascript...

Thanks,
-Jason

Bernard Marx

5:36 am on Apr 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe something else is wrong Your regular expression is OK.

var test = "Here is <p>&nbsp;</p> a test <p>&nbsp;</p> string";

alert( test.replace(/<p>&nbsp;<\/p>/g,"<br />") );

If this is HTML from innerHTML it might be good to ignore case, and allow for unexpected whitepace.

alert( test.replace(/<p>\s*&nbsp;\s*<\/p>/gi,"<br />") );