简体   繁体   English

替换字符串jquery中的所有字符

[英]Replace all characters in a string jquery

How can I replace all characters in a given string with * using the replaceAll() function? 我怎么能代替给定的字符串与所有字符*使用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> 

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. 我正在编写一个简单的hangman程序,用于隐藏带有*字符的字符串。 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. 正则表达式非常适合这样的事情,我只是不熟悉它。

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. 如果您不想使用正则表达式,可以使用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'] )) 

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> 

I don't know what the form is doing, or how you're populating words . 我不知道表格在做什么,或者你是如何填充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. 我提到这个的原因是,如果数组是通过javascript函数或jquery对象填充的,那么数组中可能会有很多不需要的条目。 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 ( * ). 这段代码正在做的是循环使用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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM