...

View Full Version : replace string with asterisk



htmlster
07-27-2011, 06:41 PM
I would like to replace all the characters within a string with asterisk using javascript.

Ex: If i have a string "test123". Replaced string should be "*******".

Could you please let me know how i can implement this using Regex within javascript.

Philip M
07-27-2011, 06:59 PM
<script type = "text/javascript">
var str = "test123";
str = str.replace(/./gi, "*"); // replace each character by an *
alert (str);
</script>
Federer's balls look like water melons out there. - Wimbledon Tennis Commentator Boris Becker, BBC1

siberia-man
07-27-2011, 07:05 PM
var str = 'test123';

// create new array with the same number of '*' as the length of the original string
str = new Array(str.length + 1).join('*');

alert(str);

Hamza7
07-27-2011, 07:12 PM
It can be easily done using split() method and for loop:


var x="test123";
var s="";
var a=x.split("");//Use an empty string to break on every character.
for (var i=0;i<a.length;i++)s+="*";

Philip M
07-27-2011, 07:15 PM
The OP asked for a regex. There are of course quite a number of other ways of achieving his objective. :)


<script type = "text/javascript">
var str = "test123";
var len = str.length;
var repstr = "";
for (var i = 0; i<len; i++) {
repstr += "*";
}
alert (repstr);
</script>

mathewfarrell35
07-27-2011, 08:18 PM
Code:
var str = 'test123';

// create new array with the same number of '*' as the length of the original string
str = new Array(str.length + 1).join('*');

alert(str);


try this it may be some what help full



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum