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