javascript 将一个单词中的所有字母替换为 js 中的 *

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13825829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:53:04  来源:igfitidea点击:

Replace all letters in a word to * in js

javascriptregexreplace

提问by I'm nidhin

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

我需要创建一个 javascript 函数,将单词的所有字母替换为星号。*它应该将单词替换hello123********。如何才能做到这一点 ?

回答by Naftali aka Neal

Use str.replace(...).

使用str.replace(...).

Many examples on the interwebs :-D

网上有很多例子:-D

For example:

例如:

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

Or in your case:

或者在你的情况下:

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

回答by UpHelix

You can just do:

你可以这样做:

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

回答by Dharman

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

试试这个链接 How to replace all characters using JavaScript for this specific case: replace 。经过 _

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

这会将每个字符替换为*空格