I wrote a regex that should match all dangerous HTML characters except <span style="background-color: #any-color">
and </span>
:
((?!<span style="background-color: #([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})">|<\/span>)[&<>"'/])
However, it matches the extra characters that I excluded.
Here RegEx should not match quotation mark style="background-color:
, but it matches:
Where did I make a mistake?
See Regex101 demo. Here is the 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];
});
}