1

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?

3 Answers 3

5

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/

Sign up to request clarification or add additional context in comments.

1 Comment

aarghhh :( :( that hurt
3

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

Comments

2
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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.