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');
alert('Method3 for:' + str);
if (valPos(str)) alert('Method3 is OK');
else alert('Method3 is KOKO');
}
function validateIllegalChars(str) {
var re1 = /[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD]/g;
var re2 = /[^[\uD800-\uDBFF][\uDC00-\uDFFF]]/g;
var str2 = str.replace(re1, '').replace(re2, '');
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;
}
function valPos(str) {
var re1 = /[\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD]/g;
var re2 = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
var str2 = str.replace(re1, '').replace(re2, '');
if (str2.length === 0) return true;
alert('str2:' + str2 + '; length: ' + str2.length);
return false;
}