in javascript how can i get a value indicating a character's general category like java Character.getType?

后端 未结 2 801
抹茶落季
抹茶落季 2021-01-22 22:13
input char:a      (unicode:97) output type:2
input char:Space  (unicode:32) output type:12

in java i can use code: \"int type = Character.getType(unico

相关标签:
2条回答
  • 2021-01-22 22:32

    Well, there's the nodeType property which will tell you if it is a text node or an HTML element, for example. As far as obtaining the unicode category, I don't believe there is a native function for that. You can try this plugin which will offer unicode support for regex:

    http://xregexp.com/plugins/

    http://www.javascriptkit.com/domref/nodetype.shtml

    0 讨论(0)
  • 2021-01-22 22:40

    There is a regexp plugin which supports Unicode categories: http://xregexp.com/plugins/.

    Using that, you could create a function that checks for each category like:

    var types = [
        'Ll', 'Lu', 'Lt', 'Lm', 'Lo', 'Mn', 'Mc', 'Me', 'Nd', 'Nl',
        'No', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Pc', 'Po', 'Sm', 'Sc',
        'Sk', 'So', 'Zs', 'Zl', 'Zp', 'Cc', 'Cf', 'Co', 'Cs', 'Cn'
    ];
    
    function getType(char) {
        var char = (char + "").charAt(0);
        for(var i = 0; i < types.length; i++) {
            if(XRegExp("\\p{" + types[i] + "}").test(char)) {
                return types[i];
            }
        }
    }
    
    alert(getType(" ")); // alerts Zs, because " " is a space separator character
    

    http://jsfiddle.net/pimvdb/mYfCZ/1/

    0 讨论(0)
提交回复
热议问题