function splitName(fullname){ var hyphenated = ['欧阳','太史','端木','上官','司马','东方','独孤','南宫','万俟','闻人','夏侯','诸葛','尉迟','公羊','赫连','澹台','皇甫', '宗政','濮阳','公冶','太叔','申屠','公孙','慕容','仲孙','钟离','长孙','宇文','城池','司徒','鲜于','司空','汝嫣','闾丘','子车','亓官', '司寇','巫马','公西','颛孙','壤驷','公良','漆雕','乐正','宰父','谷梁','拓跋','夹谷','轩辕','令狐','段干','百里','呼延','东郭','南门', '羊舌','微生','公户','公玉','公仪','梁丘','公仲','公上','公门','公山','公坚','左丘','公伯','西门','公祖','第五','公乘','贯丘','公皙', '南荣','东里','东宫','仲长','子书','子桑','即墨','达奚','褚师'], // 复姓数组 var vLength = fullname.length, // 名字长度 var surname = ''; // 姓氏 if(vLength > 2){ //取名字的前两个字,看是否在复姓库中 var preTwoWords = fullname.substr(0, 2); if($.inArray(preTwoWords, hyphenated) > -1){ surname = preTwoWords; }else{ surname = fullname.substr(0, 1); } }else if(vLength == 2){//全名只有两个字时,第一个字为姓,第二个字为名 surnameurname = fullname.substr(0, 1); }else{ surname = fullname; } // 取得姓氏后长度补* while (surname.length < vLength) { surname += '*'; } return surname; }; console.log(splitName("我"),splitName("张三"),splitName("欧阳娜娜"),splitName("张楠那"),splitName("诸葛啊")) // 我,张*,欧阳**,张**,诸葛*
如有表述不准确之处,欢迎指正,欢迎补充,感谢阅读。