How remove ul tags and all enclosed contents in JS with regex

白昼怎懂夜的黑 提交于 2020-01-15 03:53:35

问题


how i can remove with js the ul tags and all the content? example

<div>
    <ul>
         <li>a</li>
         <li>b</li>
    </ul>
</div>

i want this

<div></div>

i use

var.replace('regex', '');

anyone can help me?

Ps. only js, i can't use Jquery


回答1:


If you insist on a regular expression as the answer, you could use the following.

str = str.replace(/\s*<ul[^>]*>[\S\s]*?<\/ul>\s*/, '');



回答2:


Why use regex when you can use dom parsing?

var ul = document.getElementsByTagName('ul')[0];
ul.parentElement.removeChild(ul)

http://jsfiddle.net/8N2CQ/1/



来源:https://stackoverflow.com/questions/23542995/how-remove-ul-tags-and-all-enclosed-contents-in-js-with-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!