Skip to content

Instantly share code, notes, and snippets.

@think49
Last active December 29, 2015 21:19
Show Gist options
  • Save think49/7728995 to your computer and use it in GitHub Desktop.
Save think49/7728995 to your computer and use it in GitHub Desktop.
対象の文字タイプ(アルファベット、ひらがな、カタカナ)を取得します。
/**
* getchartypeat.js
* Get the character type (Alphabet, Hiragana, Katakana).
*
* @version 1.0.3
* @author think49
* @url https://gist.github.com/think49/7728995
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
*/
'use strict';
function getCharTypeAt (char, pos) {
var charCode = String(char).charCodeAt(Number(pos));
if (isNaN(charCode)) {
throw new TypeError(char + 'is not a string');
}
// Number (Hankaku)
if (charCode > 47 && charCode < 58) {
return 'Number';
}
// Alphabet (Hankaku)
if (charCode > 64 && charCode < 91 || charCode > 96 && charCode < 123) {
return 'Alphabet';
}
// Hiragana (Zenkaku)
if (charCode > 12352 && charCode < 12436) {
return 'Hiragana';
}
// Katakana (Zenkaku)
if (charCode > 12448 && charCode < 12541) {
return 'Katakana';
}
return 'Unknown';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment