JS/jQuery - 转换 phone 数字和电子邮件以仅显示第一个和最后一个字符
JS/jQuery - Transform phone number and email to show only first and last characters
如何从 API 响应动态转换 phone 数字和电子邮件以隐藏非极端字符?
例如:
发票将发送到您的邮箱:n*****1@gmail.com
OTP 将发送到您的 phone 号码:7******213
API 包含 key:values 所以我会得到 phone# 和电子邮件隔离,所以不需要从更大的 string.Also phone 中筛选它们数字总是 10 位数字,我需要显示第一位和最后三位数字。
PS:我不擅长正则表达式:(
要转换 phone 数字,请使用:
'740-344-4484'.replace(/(\d{1})(.*)(\d{3})/, '******')
最后,转换电子邮件:
'ScarlettAppleton@dayrep.com'.replace(/(\w{1})(.*)(\w{1})@(.*)/, '******@')
输出分别为:
"7******484"
"S******n@dayrep.com"
考虑以下使用 String.split
、String.replace
、String.slice
和 ES6 String.repeat
函数的扩展解决方案:
var email = "nick_tomson1@gmail.com", phone = "7112459213";
function transformEntry(item, type) {
switch (type) {
case 'email':
var parts = item.split("@"), len = parts[0].length;
return email.replace(parts[0].slice(1,-1), "*".repeat(len - 2));
case 'phone':
return item[0] + "*".repeat(item.length - 4) + item.slice(-3);
default:
throw new Error("Undefined type: " + type);
}
}
console.log(transformEntry(email, 'email')); // n**********1@gmail.com
console.log(transformEntry(phone, 'phone')); // 7******213
如何从 API 响应动态转换 phone 数字和电子邮件以隐藏非极端字符?
例如:
发票将发送到您的邮箱:n*****1@gmail.com
OTP 将发送到您的 phone 号码:7******213
API 包含 key:values 所以我会得到 phone# 和电子邮件隔离,所以不需要从更大的 string.Also phone 中筛选它们数字总是 10 位数字,我需要显示第一位和最后三位数字。
PS:我不擅长正则表达式:(
要转换 phone 数字,请使用:
'740-344-4484'.replace(/(\d{1})(.*)(\d{3})/, '******')
最后,转换电子邮件:
'ScarlettAppleton@dayrep.com'.replace(/(\w{1})(.*)(\w{1})@(.*)/, '******@')
输出分别为:
"7******484"
"S******n@dayrep.com"
考虑以下使用 String.split
、String.replace
、String.slice
和 ES6 String.repeat
函数的扩展解决方案:
var email = "nick_tomson1@gmail.com", phone = "7112459213";
function transformEntry(item, type) {
switch (type) {
case 'email':
var parts = item.split("@"), len = parts[0].length;
return email.replace(parts[0].slice(1,-1), "*".repeat(len - 2));
case 'phone':
return item[0] + "*".repeat(item.length - 4) + item.slice(-3);
default:
throw new Error("Undefined type: " + type);
}
}
console.log(transformEntry(email, 'email')); // n**********1@gmail.com
console.log(transformEntry(phone, 'phone')); // 7******213