方法一:
/** * 关键信息隐藏 * @param str 字符串 * @param frontLen 字符串前面保留位数 * @param endLen 字符串后面保留位数 * @returns {string} */ function hideCode(str, frontLen, endLen) { var len = str.length - frontLen - endLen; var xing = ''; for (var i = 0; i < len; i++) { xing += '*'; } return str.substring(0, frontLen) + xing + str.substring(str.length - endLen); };
方法二:
/**
* str:字符串 beginStr:开始位置 endStr:结束位置;
*/
common.desensitization = function(str,beginStr,endStr){ var len = str.length; var leftStr = str.substring(0,beginStr); var rightStr = str.substring(endStr,len); var str = '' var i = 0; try { for (i = 0; i < endStr-beginStr;i++) { str = str + '*'; } } catch (error) { } str = leftStr + str + rightStr; return str; }