Reputation: 1275
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.
Upvotes: 9
Views: 12078
Reputation: 14103
Here's a different approach using regular expressions
function hasUnicode(s) {
return /[^\u0000-\u007f]/.test(s);
}
Upvotes: 2
Reputation: 21917
A 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")
Upvotes: 11