Skip to content

Instantly share code, notes, and snippets.

@iahu
Created Aug 7, 2015
Embed
What would you like to do?
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