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

2019-07-06 02:34发布

问题:

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 ?

回答1:

Use str.replace(...).

Many examples on the interwebs :-D

For example:

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

Or in your case:

str.replace(/./g, '*');


回答2:

You can just do:

'hello123'.replace(/./g, '*');


回答3:

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