我在var content = "<li><div>list 1</div>
<ol><li><div>list nested list 1</div></li></ol></li>";
我想删除第一个</div>
内容,并将删除的内容存储在一个变量中,并将其保留在同一个变量中。如下:
content = '<ol><li><div>list nested list 1</div></li></ol></li>'
和removed_content= <li><div>list 1</div>
我尝试使用removed_content =content.replace(/</div>.*/, '');
但不接受HTML标记。
答案 0 :(得分:1)
var index = content.indexOf("</div>") + 6; //always gives you the end of first </div> tag
var removed-content = content.substr(0, index);
content = content.substr(index);
答案 1 :(得分:1)
var content = "<li><div>list 1</div><ol><li><div>list nested list 1</div></li></ol></li>";
var removed_content = "";
var reg = /^(.*?<\/div>)/;
content = content.replace(reg, function(all, $1){
removed_content = $1;
return "";
})