Created
January 31, 2013 16:53
-
-
Save gidili/4684300 to your computer and use it in GitHub Desktop.
random string masking in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.replaceAt=function(index, char) { | |
return this.substr(0, index) + char + this.substr(index+char.length); | |
} | |
var REPLACEMENT_CHAR = "*"; | |
var TO = 5; | |
var FROM = 4; | |
var str = "Hello World"; | |
var strLen = str.length; | |
var rnd = Math.floor((Math.random()*(TO - FROM + 1)) + FROM); | |
for(i = 0; i < strLen; i++){ | |
if((i+1) >= rnd && ((i+1) % rnd) == 0 && str.charAt(i) != " " && str.charAt(i) != REPLACEMENT_CHAR) | |
{ | |
str = str.replaceAt(i, REPLACEMENT_CHAR); | |
} | |
} | |
alert(str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment