我正在尝试在 React Js 中重置密码。我需要显示电子邮件已发送到他们的电子邮件 ID。我不想显示他们的整个电子邮件,而是想用 * 替换一些字符。 例如,某人有一封电子邮件 [email protected]所以我想用 * ie tes*** [email protected] 替换一些随机字符。 我怎样才能在javascript中做到这一点? 我的代码:
let a = <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32465741465b5c5572555f535b5e1c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>
let c = a.split('@');
let b = c[0].slice(0,-3)+'***'+'@'+c[1];
但这不是有效的方法。如何有效地做到这一点?
假设我有 [email protected]那么它应该显示 t** [email protected]如果 [email protected]它应该像* [email protected] .
最佳答案
使用这个:
var input = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295d4c5a5d40474e694e44484045074a4644" rel="noreferrer noopener nofollow">[email protected]</a>";
function callme(){
var a = input.split("@");
var b = a[0];
var newstr = "";
for(var i in b){
if(i>2 && i<b.length-1)newstr += "*";
else newstr += b[i];
}
console.log(newstr +"@"+ a[1]);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="callme()">click me</button>
关于javascript - 如何在javascript中用*替换@之前的电子邮件ID文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244622/