2017-11-27 15 views
1

の文字列のすべての文字をreplaceAll()関数で置き換えるにはどうすればよいですか?文字列jquery内のすべての文字を置き換えます

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は、むしろ正規表現のルートを下るないが、イムは、私がする必要があります推測。


*文字の文字列を隠し楽しみのために簡単なハングマンプログラムを書いてイム。その後、ユーザーは文字を入力し、正しい場合は文字列内の文字を元の文字に戻します。

正規表現はこのようなものには最適です。私はそれに精通していません。

+0

私はあなたが正しいと思います。 [この回答を見る](0120-13-090) – ARr0w

+0

正規表現を使用しないのはなぜですか?それは文字通り '/./g'だけです:https://jsfiddle.net/kz1m93d5/ –

+0

文字列をループし、各文字を個別に置き換えることができます。 'array [string] [counter] = '*';'(あなたの文字列を配列に格納しているからです) –

答えて

2

望むものを達成するための一つの方法です配列を文字列に変換し、map()各要素ベースを変更するdを条件とする。

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

+1

文字をマッピングすることは素晴らしい提案です。多くの感謝 –

+1

何らかの理由で2番目のパラメータ配列に数値を渡したい場合は、歓迎します。そして文字列でその番号を文字列にマップする必要があります。https:/ /jsfiddle.net/Lg0wyt9u/2520/ –

0

フォームが何をしているのか、または入力する方法がわからない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>

+0

アレイのすべての単語ではなく、配列から来るランダムな文字列の文字を置き換える方がいいでしょうか? –

1

これは、あなたが変換するsplit()を使用することができ、正規表現を使用したくない場合は、

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>

関連する問題