当前用户名称进行长度的限制,超过的长度就设置为省略号,但是目前两个字却显示是八个长度,
在查找的过程中发现,Array.from
可以查看字符串的真实长度,
不过Array.from
并不能解决我的问题,
方案:
str.replace(/[\u200b-\u200f\uFEFF\u202a-\u202e]/g, "");
但是仍然不行,
解决方案:
function stripNonPrintableAndNormalize(text, stripSurrogatesAndFormats) {
// strip control chars. optionally, keep surrogates and formats
if(stripSurrogatesAndFormats) {
text = text.replace(/\p{C}/gu, '');
} else {
text = text.replace(/\p{Cc}/gu, '');
text = text.replace(/\p{Co}/gu, '');
text = text.replace(/\p{Cn}/gu, '');
}
// other common tasks are to normalize newlines and other whitespace
// normalize newline
text = text.replace(/\n\r/g, '\n');
text = text.replace(/\p{Zl}/gu, '\n');
text = text.replace(/\p{Zp}/gu, '\n');
// normalize space
text = text.replace(/\p{Zs}/gu, ' ');
return text;
}
console.log("河豚".length);
console.log(stripNonPrintableAndNormalize("河豚", true).length);
结果:
是零宽字符的问题,