JS中实现隐藏部分姓名或者电话号码

 隐藏部分姓名

    var txt = " 卫欧尼 ";
    /* 姓名中间部分隐藏 */
    var formatName = function (name) {
        var newStr;
        if (name.length === 2) {
            newStr = name.substr(0, 1) + '*';
        } else if (name.length > 2) {
            var char = '';
            for (let i = 0, len = name.length - 2; i < len; i++) {
                char += '*';
            }
            newStr = name.substr(0, 1) + char + name.substr(-1, 1);
        } else {
            newStr = name;
        }
        return newStr;
    };

    console.log(formatName(txt.trim()));

  隐藏电话号码中间四位

var phone = " 18311110000 ";
var regPhone = /^(\d{3})\d{4}(\d{4})$/;
console.log(phone .trim().replace(regPhone, "$1****$2"))

 

你可能感兴趣的:(JS中实现隐藏部分姓名或者电话号码)