数据脱敏常用于手机号、身份证号等敏感信息中

方法一:

/**
 * 关键信息隐藏
 * @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;
}

方法三:

function desensitization(str, beginLen, endLen){
    var len = str.length;
    var firstStr = str.substr(0, beginLen);
    var lastStr = str.substr(endLen);
    var middleStr = str.substring(beginLen, len-Math.abs(endLen)).replace(/[\s\S]/ig, '*');

    tempStr = firstStr+middleStr+lastStr;

    return tempStr;

}
desensitization('12345678901234',4, -4);

方法四:

数字类型

certificatecodecopy = certificatecode.replace(/^(.{6})(?:\d+)(.{4})$/,  "\$1****\$2");
所有类型

enginenocopy = engineno.replace(/^(.{2})(?:\w+)(.{1})$/, "\$1****\$2");


enginenocopy = engineno.replace(/^(.{4})(?:\w+)(.{4})$/, "\$1****\$2");

// vue中使用
<el-table-column
                  width="200px"
                  prop="credentialsNum"
                  label="身份证号码"
                  align="center"
                >
                  <template slot-scope="scope">
                  <p v-if="scope.row.credentialsNum != null && scope.row.credentialsNum != '' ">
                    {{scope.row.credentialsNum.replace(/^(.{6})(?:\d+)(.{4})$/, "$1****$2")}}
                  </p>
                  <p v-else>未知</p>
                  </template>
                </el-table-column>

  

posted on 2020-08-14 17:45  ygunoil  阅读(8201)  评论(0编辑  收藏  举报