123456789101112131415161718192021222324252627282930 |
- /**
- * 处理字符串为*格式,中间显示自定义*号
- * str 需要处理的字符串
- * startLength 前面显示的字符串长度
- * endLength 后面显示的字符串长度
- */
- var sub = function(str, startLength, endLength) {
- if (str.length == 0 || str == undefined) {
- return "";
- }
- var length = str.length;
- if (length >= startLength + endLength) {
- //判断当字符串长度为二时,隐藏末尾
- if (length === 2) {
- return str.substring(0, 1) + '*';
- }
- else if (3 <= length && length <= 10){
- return str.substring(0, 1) + '**';
- }
- //判断字符串长度大于首尾字符串长度之和时,隐藏中间部分
- else if (length >= 11) {
- return str.substring(0, startLength) + "****" + str.substring(length - endLength, length);
- } else {
- return str
- }
- }
- }
- module.exports = {
- sub: sub
- }
|