Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all letters in a word to * in js [closed]

I need to create a javascript function that replaces all letters of a word to an asterisk * .It should replace the word hello123 to ********. How can this be done ?

like image 882
I'm nidhin Avatar asked Dec 11 '12 17:12

I'm nidhin


3 Answers

You can just do:

'hello123'.replace(/./g, '*');
like image 170
UpHelix Avatar answered Oct 26 '22 00:10

UpHelix


Use str.replace(...).

Many examples on the interwebs :-D

For example:

str.replace('foo', 'bar');

Or in your case:

str.replace(/./g, '*');
like image 29
Naftali Avatar answered Oct 25 '22 22:10

Naftali


Try this link How to replace all characters in a string using JavaScript for this specific case: replace . by _

var s1 = s2.replace(/\S/gi, '*');

which will replace anything but whitespace or

var s1 = s2.replace(/./gi, '*');

which will replace every single character to * even whitespace

like image 43
Dharman Avatar answered Oct 26 '22 00:10

Dharman