Skip to content

Instantly share code, notes, and snippets.

@iahu
Created August 7, 2015 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iahu/ae591399c29526d2e96a to your computer and use it in GitHub Desktop.
Save iahu/ae591399c29526d2e96a to your computer and use it in GitHub Desktop.
js获取汉字声母
var getShengmu = (function () {
var idx = -1;
var MAP = 'ABCDEFGHJKLMNOPQRSTWXYZ';
var boundaryChar = '驁簿錯鵽樲鰒餜靃攟鬠纙鞪黁漚曝裠鶸蜶籜鶩鑂韻糳';
if ( !String.prototype.localeCompare ) {
throw Error('String.prototype.localeCompare not supported.');
}
return function getShengmu(c) {
if ( /[^\u4e00-\u9fa5]/.test(c) ) {
return c;
}
for (var i = 0; i < boundaryChar.length; i++) {
if (boundaryChar[i].localeCompare( c, 'zh-CN-u-co-pinyin' ) >= 0) {
idx = i;
break;
}
}
return MAP[idx];
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment