JSFiddle - React, Tailwind, and code Playground
HTML
<input type="button" onclick="doCheck('eo' + String.fromCharCode(65537));" value="click">
JavaScript
function doCheck(str) {
alert('Method1 for:' + str);
if (validateIllegalChars(str)) alert('is OK');
else alert('in KOKO');
alert('Method2 for:' + str);
if (val2(str)) alert('Method2 is OK');
else alert('Method2 is KOKO');
}
function validateIllegalChars(str) {
var re = /[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD]/g;
var str2 = str.replace(re, '');
alert('str2:' + str2);
if (str2 != str) return false;
return true;
}
function val2(str) {
var re2 = /[^\u0009\r\n\u0020-\uD7FF\uE000-\uFFFD]/g;
var str2 = str.replace(re2, '');
alert('str2:' + str2);
if (str2 != str) return false;
return true;
}