I have a string such like that : "Xin chào tất cả mọi người". There are some Unicode characters in the string. All that I want is writing a function (in JS) to check if there is at least 1 Unicode character exists.
I have a string such like that : "Xin chào tất cả mọi người". There are some Unicode characters in the string. All that I want is writing a function (in JS) to check if there is at least 1 Unicode character exists.
Share Improve this question edited Jul 20, 2017 at 19:10 deceze♦ 522k88 gold badges798 silver badges939 bronze badges asked Feb 12, 2014 at 4:30 Nấm LùnNấm Lùn 1,2756 gold badges28 silver badges48 bronze badges 1- 3 JavaScript strings do not "contain UTF-8" characters. They contain Unicode code-points (encoded as one code-point/character for Unicode in the BMP - whatever UTF-16/UCS-2 internal coding is an entirely different can of worms). So, now what is a "UTF-8 character"? Do you mean Unicode character not in the ASCII plane? – user2864740 Commented Feb 12, 2014 at 4:37
2 Answers
Reset to default 11A string is a series of characters, each which have a character code. ASCII defines characters from 0 to 127, so if a character in the string has a code greater than that, then it is a Unicode character. This function checks for that. See String#charCodeAt.
function hasUnicode (str) {
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 127) return true;
}
return false;
}
Then use it like, hasUnicode("Xin chào tất cả mọi người")
Here's a different approach using regular expressions
function hasUnicode(s) {
return /[^\u0000-\u007f]/.test(s);
}