What I want to do is for example:
var variable = "Hello"
becomes
*****
i.e each letter replaced with an asterisk. In JS please.
What I want to do is for example:
var variable = "Hello"
becomes
*****
i.e each letter replaced with an asterisk. In JS please.
Repeat *
for the length of the input by way of an array
> var variable = "Hello"
> Array(variable.length + 1).join("*");
> "*****"
Try this may help you..
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/[^abc]/g, "*");
document.getElementById("demo").innerHTML = res;
}
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Hello" with "*****" </p>
<p id="demo">Hello</p>
<button onclick="myFunction()">Try it</button>
</body>
</html>
This answer can be helpful.
You can update the string with a new one that has as many characters as the initial string:
var str = "Hello World!";
var my_char = "*";
str = Array(str.length+1).join(my_char);
Adding a ES6 answer, to the ES5 one:
var foo = "Hello";
var bar = "*".repeat(foo.length);
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat