【问题标题】:Replace all characters in a string jquery替换字符串jquery中的所有字符
【发布时间】:2017-11-27 19:52:08
【问题描述】:

如何使用replaceAll() 函数将给定字符串中的所有字符替换为*

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>

我宁愿不走正则表达式路线,但我猜我可能不得不这样做。


我正在编写一个简单的 hangman 程序来隐藏带有* 字符的字符串。然后用户输入一个字符,如果正确,则字符串中的字符将重置回其原始字符。

正则表达式非常适合这样的东西,我只是不太熟悉它。

【问题讨论】:

  • 我猜你是对的。 see this answer
  • 为什么不直接使用正则表达式?它实际上只是/./g jsfiddle.net/kz1m93d5
  • 您可以遍历字符串并单独替换每个字符,例如array[string][counter] = '*';(因为您将字符串存储在数组中)
  • @craig_h 我正在编写一个简单的“hangman”程序来隐藏带有 * 字符的字符串。然后用户输入一个字符,如果正确,则字符串中的字符将重置回其原始字符。正则表达式非常适合这样的东西,我只是不太熟悉它。
  • @MasterYoda 啊,好的。然后我会遍历单词中的字符来创建掩码:jsfiddle.net/qqL9o129

标签: javascript jquery


【解决方案1】:

如果您不想使用正则表达式,可以使用split() 将字符串转换为数组,然后使用map() 根据条件修改每个元素。

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'] ))

【讨论】:

  • 不客气,如果由于某种原因您想在第二个参数数组中传递数字并在字符串中找到然后您还需要将该数字映射到字符串,就像jsfiddle.net/Lg0wyt9u/2520
【解决方案2】:

这是实现你想要的一种方式

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>

【讨论】:

    【解决方案3】:

    我不知道表单在做什么,也不知道您如何填充words。我提到这一点的原因是,如果数组是通过 javascript 函数或 jquery 对象填充的,则数组中可能有很多不需要的条目。您可能需要更改以下代码以忽略那些不需要的条目。

    这段代码所做的是循环遍历words 数组并替换每个字符(正则表达式/./g)并用星号替换它(*)。

    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>

    【讨论】:

    • 替换来自数组的随机字符串中的字符而不是替换数组中的每个单词会更好吗?
    猜你喜欢
    • 2012-11-14
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多