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

2019-07-06 02:09发布

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 ?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-06 02:35

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

查看更多
等我变得足够好
3楼-- · 2019-07-06 02:42

Use str.replace(...).

Many examples on the interwebs :-D

For example:

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

Or in your case:

str.replace(/./g, '*');
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-07-06 02:42

You can just do:

'hello123'.replace(/./g, '*');
查看更多
登录 后发表回答