最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unicode - How to know there is any UTF8 character in a string with Javascript? - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 11

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")

Here's a different approach using regular expressions

function hasUnicode(s) {
    return /[^\u0000-\u007f]/.test(s);
}
发布评论

评论列表(0)

  1. 暂无评论