1

How can I replace all characters in a given string with * using the replaceAll() function?

var words = ['marvel','computer','regex','throne'];
var rand = words[Math.floor(Math.random() * words.length)];
//should replace all characters in the string with *
$('#value').text(rand).replaceAll(rand,'*');
.container{
   border: 1px solid black;
   padding: 5px;
   margin: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
  <input type='text' />
  <span id='value'></span>
  <hr />
  <button type='button' id='submit'>Submit</button>
</div>

Id rather not go down the regex route but Im guessing I may have to.


Im writing a simple hangman program for fun that hides the string with * characters. The user then enters a character and if correct the character in the string is reset back to its original.

regex is great for stuff like this, im just not that familiar with it.

6
  • i guess you are correct. see this answer Commented Nov 27, 2017 at 11:20
  • Why not just use regex? It's literally just /./g : jsfiddle.net/kz1m93d5 Commented Nov 27, 2017 at 11:24
  • You could loop through the string and replace each character individually, something like array[string][counter] = '*'; (since you are storing your strings in an array) Commented Nov 27, 2017 at 11:26
  • @craig_h Im writing a simple 'hangman' program for fun that hides the string with * characters. The user then enters a character and if correct the character in the string is reset back to its original. regex is great for stuff like this, im just not that familiar with it. Commented Nov 27, 2017 at 11:29
  • @MasterYoda Ah, OK. Then I would just loop over the characters in the word to create the mask: jsfiddle.net/qqL9o129 Commented Nov 27, 2017 at 11:39

3 Answers 3

2

If you don't want to use regex you could use split() to transform string to array and then map() to modify each element based on condition.

let replaceAll = (str, chars = []) => {
  return str.split('')
  .map(c => c.trim() && !chars.includes(c) ? '*' : c)
  .join('');
}
  
console.log(replaceAll('lorem ipsum', ['l', 'm']))
console.log(replaceAll('123    lorem ips', ['2', 'i'] ))

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome, if for some reason you want to pass numbers in second parameter array and find then in string you then also need to map that number to string like so jsfiddle.net/Lg0wyt9u/2520
1

This is one way to achieve what you want

var words = ['marvel','computer','regex','throne'];
var rand = words[Math.floor(Math.random() * words.length)];
//should replace all characters in the string with *
var maskedRand = ''
for (var char in rand) {
  maskedRand += '*'
}
$('#value').text(maskedRand);
.container{
   border: 1px solid black;
   padding: 5px;
   margin: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
  <input type='text' />
  <span id='value'></span>
  <hr />
  <button type='button' id='submit'>Submit</button>
</div>

Comments

0

I don't know what the form is doing, or how you're populating words. The reason I mention this, is if the array is being populated via a javascript function or jquery object, there may be a LOT of unwanted entries in the array. You may want to alter the below code to ignore those unwanted entries.

What this code is doing is looping through the words array and replacing each character (regex /./g) and replacing it with an asterisk (*).

var words = ['marvel','computer','regex','throne'];
var rand = words[Math.floor(Math.random() * words.length)];
//should replace all characters in the string with *
for(var i in words) {
    words[i] = words[i].replace(/./g, '*');
}
console.log(words);
.container{
   border: 1px solid black;
   padding: 5px;
   margin: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
  <input type='text' />
  <span id='value'></span>
  <hr />
  <button type='button' id='submit'>Submit</button>
</div>

1 Comment

Wouldnt it be better to replace the characters in random string that comes from the array rather than every word in the array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.