开发者

IsControl in Javascript

开发者 https://www.devze.com 2023-03-14 02:58 出处:网络
Is there a function like .Net\'s Char.IsControl() in Javascript? Also, I find it very in开发者_开发技巧convenient that Javascript doesn\'t have an official web site like Python, Java and C#. What do y

Is there a function like .Net's Char.IsControl() in Javascript?

Also, I find it very in开发者_开发技巧convenient that Javascript doesn't have an official web site like Python, Java and C#. What do you use as reference?


No, there isn't one. However, JavaScript uses UTF-16 and so you can use the Unicode definition of a control character to write your own. That definition is:

Control Codes The 65 characters in the ranges U+0000..U+001F and U+007F..U+009F. Also known as control characters.

Thus:

function isControl(ch) {
    var code = ch.charCodeAt(0);
    return ((code >= 0x0000 && code <= 0x001f) || (code >= 0x007f && code <= 0x009f));
}

Or as a regexp:

var isControlRex = /[\u0000-\u001F\u007F-\u009F]/;
function isControl(ch) {
    return isControlRex.test(ch);
}

I use the specification as a reference. Also the Mozilla Developer Network's (what we used to call the MDC) JavaScript pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消