Alvaro
Alvaro

Reputation: 41605

Replacing empty <p></p> with regex

I'm trying to get rid of empty paragraphs using replace with a regex, but not having luck.

return text.replace('/(<p><\/p>)+/g', '');

I've tested the regex /(<p><\/p>)+/g in regex101 and it seems to be ok. What am I doing wrong?

Reproduction online

Upvotes: 4

Views: 1032

Answers (2)

Wainage
Wainage

Reputation: 5412

Almost there ... but a regex in JS isn't a string

Your's

return text.replace('/(<p><\/p>)+/g', '');

Try this

return text.replace(/(<p><\/p>)+/g, '');

Upvotes: 5

Lulylulu
Lulylulu

Reputation: 1264

You should remove the quotes:

return text.replace(/(<p><\/p>)+/g, '');

Upvotes: 7

Related Questions