0

I'm trying to clean up a string of text on the server side from the output generated by a wysiwyg. and while I can fix it client side, it's best to also fix this on the server side.

var string = "<p>firstline</p><p>secondline</p><p>thirdline</p><p>iframe</p><p>a</p><p>df</p><p>dsf&nbsp;</p><p><br></p><p>sd</p><p>f</p><p>sdf</p><p><br></p>"

var x = string.replace("<p><br></p>", "");

https://jsfiddle.net/8c0yh9r7/

the code should but doesn't get rid of the break within the paragraphs

why is that?

1

3 Answers 3

3

Use a regex with a global flag, like:

 string.replace(/<p><br><\/p>/g, "");

https://jsfiddle.net/Lu2r3820/1/

When using a string only the first occurrence will be replaced.

See replace() documentation

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

Comments

2

doesn't get rid of the break within the paragraphs

Yes, it does… but only once. You have more than one paragraph containing a line break in your code.

If you want to replace it more than once, you need to use a regex and mark it as global with g.

var x = string.replace(/<p><br><\/p>/g, "");

Comments

1

It does replace, but only the first occurrence. If you run this afterwards, you can see the second occurrence disappearing.

var x = x.replace("<p><br></p>", "");

refer to this to replace all occurrences. How to replace all occurrences of a string in JavaScript?

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.