使用正则表达式删除所有 html 属性(替换)
Remove all html attributes with regex (replace)
例如,我有这样的html:
<title>Ololo - text’s life</title><div class="page-wrap"><div class="ng-scope"><div class="modal custom article ng-scope in" id="new-article" aria-hidden="false" style="display: block;"><div class="modal-dialog first-modal-wrapper">< div class="modal-content"><div class="modal-body full long"><div class="form-group">olololo<ul style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);"><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li></ul><p style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);">bbcvbcvbcvbcvbcvbcvbcvb</p></div></div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div class="page-wrap"></div></div>
如何从此类 HTML 中删除所有样式类 ID 等?
我有这样的正则表达式:
/<([a-z][a-z0-9]*)[^>]*?('/?)>/i
怎么了?如何在正则表达式的帮助下删除所有 HTML 属性?
这是小提琴:
http://jsfiddle.net/qL4maxn0/1/
首先,我建议您在这种情况下不要使用正则表达式,它们不是用来解析像 HTML 这样的树形结构的。
但是,如果您别无选择,我认为对于请求的问题,您可以使用正则表达式。
在我看来,你忘记了空格、口音等。您可以使用不允许将大于 >
和小于 <
符号的事实用作原始文本。
/<'s*([a-z][a-z0-9]*)'s.*?>/gi
并调用它:
result = body.replace(regex, '<$1>')
对于给定的样本,它会产生:
<title>Ololo - text’s life</title><div><div><div><div><div><div><div>olololo<ul><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li></ul><p>bbcvbcvbcvbcvbcvbcvbcvb</p></div></div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div></div></div>
你不应该在这里使用正则表达式。
var html = '<title>Ololo - text’s life</title><div class="page-wrap"><div class="ng-scope"><div class="modal custom article ng-scope in" id="new-article" aria-hidden="false" style="display: block;"><div class="modal-dialog first-modal-wrapper"><div class="modal-content"><div class="modal-body full long"> <div class="form-group">olololo<ul style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);"><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li> </ul><p style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);">bbcvbcvbcvbcvbcvbcvbcvb</p></div><div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div class="page-wrap"></div></div>';
var div = document.createElement('div');
div.innerHTML = html;
function removeAllAttrs(element) {
for (var i = element.attributes.length; i-- > 0;)
element.removeAttributeNode(element.attributes[i]);
}
function removeAttributes(el) {
var children = el.children;
for (var i = 0; i < children.length; i++) {
var child = children[i];
removeAllAttrs(child);
if (child.children.length) {
removeAttributes(child);
}
}
}
removeAttributes(div);
console.log(div.innerHTML);
工作小提琴
源
您缺少使替换全局的g
标志。
/<([a-z][a-z0-9]*)[^>]*?('/?)>/ig
此外,如果您出于安全目的这样做,请考虑使用适当的 HTML 清理器:在客户端清理/重写 HTML
相关文章:
- 替换标记中的属性
- 将css属性替换为变量
- angular2形式-无法读取未定义的属性替换
- 如何将 JavaScript Yahoo Weather API 调用属性替换为变量
- 无法读取未定义的属性“替换” - 替换 JS 生成的 HTML 中的文本
- 用 LESS 伪造一个 css 属性(并在解析时用一个真实属性替换它)
- 使用正则表达式删除所有 html 属性(替换)
- 用对象属性替换数组元素
- 无法读取未定义的属性替换
- 将一个对象的属性替换为另一个对象的属性
- 为什么画布中以前的圆圈的属性被较新的属性替换
- jquery autocomplete 无法读取未定义的属性“替换”
- JS getters:定义属性替换或补充旧的内联“get”“set”语法
- 用户脚本 - 将变量的属性替换为函数
- 使用JavaScript用src属性替换多个图像
- 元素's src属性替换,以便在加载时首先呈现修改后的元素
- jQuery IE9-10:无法获得未定义或空引用的属性替换
- 用数据属性替换不同类名的一部分
- 将src属性替换为javascript– 获得相对路径
- 如何删除所有下拉选项并仅通过给定的NAME属性替换新选项