最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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, '');
发布评论

评论列表(0)

  1. 暂无评论