/*去掉html标签(真正意义上去掉所有html标签包括内嵌的css样式)*/ String.prototype.stripHTML = function (isRemoveNewLine) { let t = document.createElement("div"); t.innerHTML = html; let r = t.innerText; isRemoveNewLine && (r = r.replace(/[\r\n]/g, "")); return r; } //测试---------------------------------------- '<b>123</b><p>456</p>'.stripHTML();//'123\n\n456' '<b>123</b><p>456</p>'.stripHTML(true);//'123456'
/*去掉a标签*/ String.prototype.stripA = function () { return this.replace(new RegExp("</?a.*?>", "g"), ""); }; //测试---------------------------------------- '<b>1<a>2</a>3</b><p>456</p>'.stripA();//'<b>123</b><p>456</p>'
/*去掉style样式*/ String.prototype.stripStyle = function () { return this.replace(/style=/g, "disableStyle=").replace(/href=/g, "disableHref=") }; //测试---------------------------------------- '<a style="color:red" href="http://shuzhiqiang.com">123</a>'.stripStyle(); //'<a disableStyle="color:red" disableHref="http://shuzhiqiang.com">123</a>'
/*是否为html代码*/ function isHTMLcode(htmlStr) { var reg = /<[^>]+>/g; return reg.test(htmlStr); } //测试---------------------------------------- console.log(isHTMLcode("<b>123</b>"));//true