javascript - Replacing empty <p><p> with regex - Stack Overflow

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

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 empty textContent 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
Add a ment  | 

2 Answers 2

Reset to default 7

You 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, '');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744618979a4584226.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信