Friday 15 July 2011

javascript - How to replace a string according to their length with "*" (astrik) letter -



javascript - How to replace a string according to their length with "*" (astrik) letter -

from back-end json data, receiving "password" infromation. requested hide password info in html page.

i know there way hide using input type "password". in html showing details hiding password.

i trying replace string using regexp method. not working.

here try:

var st = "shchool"; //it 7 letters, need print 7 '*' st.replace(/./g, "*"); // trying replace. console.log(st);

the method string.replace() homecoming new string replacing text, , doesn't modify string apply method.

var st = "shchool"; st.replace(/./g, "*"); // returns new string "*******"

you need assign result variable, if want alter it:

st = st.replace(/./g, "*"); // assign replaced string st

now can log string:

console.log(st);

mdn - string.replace() returns new string

consider masking password * in back-end, replacing in front-end isn't idea!

test code

class="snippet-code-js lang-js prettyprint-override">var st = "shchool"; // has 7 letters, need print 7 '*' st.replace(/./g, "*"); // variable st not modified console.log("log1:",st); st = st.replace(/./g, "*"); // assign homecoming value st console.log("log2",st); // @ console

javascript regex

No comments:

Post a Comment