It would be great if this implementation was validated against many possible attacks. https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet is a good starting point.