Created
August 7, 2015 06:37
-
-
Save iahu/ae591399c29526d2e96a to your computer and use it in GitHub Desktop.
js获取汉字声母
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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