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
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
Share Improve this question asked Mar 30, 2016 at 15:42 AlvaroAlvaro 41.6k31 gold badges172 silver badges348 bronze badges 3-
1
You'd be better off finding
p
elements with emptytextContent
and removing them. – ssube Commented Mar 30, 2016 at 15:43 -
you don't need quotes
'
, or the+
– JKirchartz Commented Mar 30, 2016 at 15:45 - Possible duplicate of RegEx match open tags except XHTML self-contained tags – Maytham Fahmi Commented Mar 30, 2016 at 15:47
2 Answers
Reset to default 7You should remove the quotes:
return text.replace(/(<p><\/p>)+/g, '');
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, '');