Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In trying to replace parts of a string using a regex.

This is my string

"<p>0</p>
<p>0</p> 
<p>&nbsp;</p>
<p>1</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>2</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>3</p>"

and im doing this

aboveString.replace('/<p>\&nbsp;<\/p>/g','<br style="clear:both;" />')

I need to replace all instances of the

<p>&nbsp;</p>

to be replaced with

<br style="clear:both;" />

Can you please tell me where am i going wrong?

share|improve this question

3 Answers

up vote 4 down vote accepted

Your regex '/<p>\&nbsp;<\/p>/g' isn't a regex, it's a string. Remove the quotes to make it a regex literal:

aboveString.replace(/<p>\&nbsp;<\/p>/g,'<br style="clear:both;" />')

Demo: http://jsfiddle.net/pTqgX/

share|improve this answer
1  
aarghhh :( :( that hurt – ghostCoder Sep 7 at 13:30
aboveString.replace(/<p>\&nbsp;<\/p>/g,'<br style="clear:both;" />')

in Javascript u don't write regexp inside "". Every thing written within "" is treated as String. :)

share|improve this answer

I know, its about Regex, but its so obviously doable without Regex:

aboveString.split('<p>&nbsp;</p>').join('<br style="clear:both;" />');

Fiddle

Now downvote me, friends

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.