JSでは一部の名前や電話番号を隠します.

802 ワード

名前の一部を隠す
    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()));
  電話番号の真ん中の4人を隠します.
var phone = " 18311110000 ";
var regPhone = /^(\d{3})\d{4}(\d{4})$/;
console.log(phone .trim().replace(regPhone, "$1****$2"))