How to find whether a particular string has unicode characters (esp. Double Byte characters)

后端 未结 6 858
我在风中等你
我在风中等你 2020-12-08 13:55

To be more precise, I need to know whether (and if possible, how) I can find whether a given string has double byte characters or not. Basically, I need to open a pop-up to

6条回答
  •  自闭症患者
    2020-12-08 14:47

    Here is benchmark test: http://jsben.ch/NKjKd

    This is much faster:

    function containsNonLatinCodepoints(s) {
        return /[^\u0000-\u00ff]/.test(s);
    }
    

    than this:

    function isDoubleByte(str) {
        for (var i = 0, n = str.length; i < n; i++) {
            if (str.charCodeAt( i ) > 255) { return true; }
        }
        return false;
    }
    

提交回复
热议问题