从后端json数据,我收到"密码"信息.但是我要求在html页面中隐藏密码信息.
我知道有一种方法可以隐藏输入类型"密码".但是在这个html中我只是显示隐藏密码的细节.
我试图使用regexp方法替换字符串.但没有工作.
这是我的尝试:
var st = "Shchool"; //it is 7 letters, i need to print 7 '*'
st.replace(/./g, "*"); // i am trying to replace.
console.log(st);
Run Code Online (Sandbox Code Playgroud)
该方法String.replace()返回带有替换文本的新字符串,并且不会修改应用该方法的字符串.
var st = "Shchool";
st.replace(/./g, "*"); // returns new string "*******"
Run Code Online (Sandbox Code Playgroud)
如果要更改结果,则需要将结果分配给变量:
st = st.replace(/./g, "*"); // assign the replaced string back to st
Run Code Online (Sandbox Code Playgroud)
现在您可以记录字符串:
console.log(st);
Run Code Online (Sandbox Code Playgroud)
MDN - String.replace()返回一个新字符串
考虑*在后端屏蔽密码,在前端替换它不是一个好主意!
var st = "Shchool"; // It has 7 letters, I need to print 7 '*'
st.replace(/./g, "*"); // The variable st is NOT modified
console.log("LOG1:",st);
st = st.replace(/./g, "*"); // Assign the return value to st
console.log("LOG2",st);
// Look at the consoleRun Code Online (Sandbox Code Playgroud)