mercredi 7 février 2018

Replace function in javascript By variable

i need to replace $ by Random how can i do this i want to convert this text from "Some texte here some text here" to be like this "Som(gb5t2)e t(gb95f)ext(ze6f5)e h(bs3e2)ere (avFe3)so(gEC3e)me (aFE98)tex(8FZq2)t h(dq9aA)ere(cZ3cC)"

function String() {
var str =  "Some texte here some text here".replace(/(.{3})/g, "$1$");
var mystring = str;
var find = "$";
var regex = new RegExp(find, "g");
var fun = randomString();
alert(mystring.replace(regex, fun));
document.write(str);

function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 5;
var randomstring = '';
for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
}
document.randform.randomfield.value = " ( " + randomstring + " ) ";

}

}




Aucun commentaire:

Enregistrer un commentaire