JS RegExp删除所有HTML标签及其内容?
例如,我需要从中得到:
Before Bold In Bold After Bold
要得到:
Before Bold After Bold.
我试过了:
string.replace(/.*/,'')
但它不能按预期工作。
试试这个:
string.replace(/<([^>]+?)([^>]*?)>(.*?)<\/\1>/ig, "")
它对我有用。
看到它在这里工作
var div = document.createElement("div"), result = "", child; div.innerHTML = str; child = div.firstChild; do { if (child.nodeType === 3) { result += child.nodeValue; } } while (child = child.nextSibling); console.log(result);
我不确定正则表达式,但使用jQuery你可以轻松删除子项并使用经典的单行返回HTML:
string = $('').html(string).children().remove().end().html();