String.prototype.substr2=function(a,b){var s = this.replace(/([^\x00-\xff])/g,"\x00$1");return(s.length<b)?this:s.substring(a,b).replace(/\x00/g,'');}
大于255的Ascii码 转换成 \x00$1 ‘字符串'.length==3 //true
‘字符串’.replace(/([^\x00-\xff])/g,'\x00$1').length==6 //true
本文介绍了一种使用JavaScript处理Unicode字符的技巧,通过扩展String.prototype来实现对特定ASCII范围外字符的处理,使得字符串长度计算更加准确,并展示了如何通过正则表达式替换大于255的ASCII码。
346

被折叠的 条评论
为什么被折叠?



