我写了一个正则表达式,它应该匹配除<span style="background-color: #any-color">
和</span>
之外的所有危险HTML字符:
((?!<span style="background-color: #([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})">|<\/span>)[&<>"'/])
但是,它与我排除的多余字符匹配。
此处RegEx不应与引号style="background-color:
匹配,但应匹配:
我在哪里弄错了?
请参见Regex101 demo。这是link to the current project:
function escapeHtml(in_) {
return in_.replace(/((?!<span style="background-color: #([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})">|<\/span>)[&<>"'/])/g,s => {
const entityMap = {
'&': '&','<': '<','>': '>','"': '"','\'': ''','/': '/',};
return entityMap[s];
});
}