JS: how to remove "spammy invisible chars"?

Viewed 339

Some user is flooding with some chars that bypass regex filters. when I paste that chars into UTF8 editor, they look same (except the flood version is not selectable completly: it seems to be some invisible chars inserted

enter image description here

And when you switch to ANSI encodage, you clearly see the difference of the 2 words liebehomo lâ€iâ€ebâ€ehâ€oâ€mo

When I paste that spammy word into developer tool, I get

enter image description here

s.length gives 14 and not 9 !

enter image description here

So my question is: how would it be possible to filter these spammy words that contains some strange chars ?

1 Answers

Probably as simple as replacing any non-printable character first:

string = string.replace(/[^ -~]+/g, "");

document.getElementById('demo').addEventListener('input', function(e) {
    e.target.innerHTML = e.target.innerHTML.replace(/[^ -~]+/g, "");
    console.log(e.target.innerHTML);
});
<textarea id="demo"></textarea>

Related