Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中创建.replace循环?_Javascript_For Loop_Replace - Fatal编程技术网

如何在javascript中创建.replace循环?

如何在javascript中创建.replace循环?,javascript,for-loop,replace,Javascript,For Loop,Replace,我目前正试图在javascript中创建一个.replace函数循环。我想做的是用连字符替换一个随机字符,但我正在努力解决的部分是如何使它循环替换字符到连字符。以下是我目前掌握的代码: var randHold; var randomWord; var randLetHold; var dispWord; var repLetHold; var myWords = new Array("Spectrometer", "Bandwagonjghvy

我目前正试图在javascript中创建一个.replace函数循环。我想做的是用连字符替换一个随机字符,但我正在努力解决的部分是如何使它循环替换字符到连字符。以下是我目前掌握的代码:

    var randHold;
    var randomWord;
    var randLetHold;
    var dispWord;
    var repLetHold;

    var myWords = new Array("Spectrometer", "Bandwagonjghvyjh", "jvjyvyvilvyjlvyv", 
                            "fruitjjyvtyvjv", "seventctcvtv", "weathertfhtcthc", 
                            "undercfxdtfv"); // random letters to make words that have more than 10 letters

    function level() { 
        randHold = parseInt((Math.random() * 6) + 1);//code to randomly pick a word from the above array
        randomWord = myWords[randHold]; //code to call the random word from the array
        randLetHold = (Math.random() * randomWord.length);//code to randomly pick a character from the random word chosen
        repLetHold = randomWord.charAt(randLetHold);//code to call the random character
        for (i = 1; i <= 3; i++) //loop to replace three random characters with a hyphen 
        {
            dispWord = randomWord.replace(repLetHold," - ");//code to replace a random character with a hyphen
            document.write(dispWord);//But all this does is display the word(with ONE hypenated character)three times.
        }

    }
var-randHold;
变异随机词;
var randLetHold;
var-dispWord;
var-repLetHold;
var myWords=新阵列(“光谱仪”、“波段JGHVYJH”、“JVJYVILVYJLVYV”,
“水果JJYVTYVJV”、“第七台CTCVTV”、“weathertfhtcthc”,
“低于CFXDTFV”);//“随机字母”可生成包含10个以上字母的单词
函数级别(){
randHold=parseInt((Math.random()*6)+1);//从上述数组中随机选取一个单词的代码
randomWord=myWords[randHold];//从数组中调用随机词的代码
randLetHold=(Math.random()*randomWord.length);//从所选的随机单词中随机选取字符的代码
repLetHold=randomWord.charAt(randLetHold);//调用随机字符的代码

对于(i=1;i如果使用正则表达式,则可以使用
g
(全局)标志一次替换所有实例。例如:

var str = "this is a mass Spectrometer, which is a Spectrometer to detect the spectra of different masses";
var replaced = str.replace(/Spectometer/g, 'something');
// "this is a mass something, which is a something to detect the spectra of different masses";

请记住,在正则表达式中。

对于要在单词中连字符的3个随机字符,您需要这样的内容

<div id="result"></div>

var myWords = ["Spectrometer", "Bandwagonjghvyjh", "jvjyvyvilvyjlvyv",
    "fruitjjyvtyvjv", "seventctcvtv", "weathertfhtcthc",
    "undercfxdtfv"]; // random letters to make words that have more than 10 letters

var randomWord;
var dispWord;
var repLetHold = [];

function uniqueCount(str) {
    var unique = [];

    Array.prototype.forEach.call(str, function (value) {
        if (unique.indexOf(value) === -1) {
            unique.push(value);
        }
    });

    return unique.length;
}

function level() {
    var randHold = Math.floor(Math.random() * myWords.length);

    dispWord = randomWord = myWords[randHold];
    if (uniqueCount(randomWord) > 2) {
        var count = 0,
            temp1,
            temp2;

        while (count < 3) {
            temp1 = Math.floor(Math.random() * dispWord.length);

            temp2 = dispWord.charAt(temp1);
            if (temp2 !== "-" && repLetHold.indexOf(temp2) === -1) {
                dispWord = dispWord.replace(new RegExp(temp2, "g"), "-");
                repLetHold[count] = temp2;
                count += 1;
            }
        }
    }

    document.getElementById("result").textContent = dispWord;
}

level();

console.log(randomWord, repLetHold);

var myWords=[“光谱仪”、“波段JGHVYJH”、“JVJYVILVYJLVYV”,
“水果JJYVTYVJV”、“第七台CTCVTV”、“weathertfhtcthc”,
“undercfxdtfv”];//随机字母,使单词包含10个以上的字母
变异随机词;
var-dispWord;
var repLetHold=[];
函数唯一计数(str){
var-unique=[];
Array.prototype.forEach.call(str,function(value){
if(唯一的索引of(值)=-1){
唯一。推送(值);
}
});
返回唯一的.length;
}
功能级别(){
var randHold=Math.floor(Math.random()*myWords.length);
dispWord=randomWord=myWords[randHold];
如果(唯一计数(随机字)>2){
变量计数=0,
temp1,
temp2;
而(计数<3){
temp1=Math.floor(Math.random()*dispWord.length);
temp2=dispWord.charAt(temp1);
if(temp2!=“-”&&replethod.indexOf(temp2)==-1){
dispWord=dispWord.replace(新的RegExp(temp2,“g”),“-”;
repLetHold[count]=temp2;
计数+=1;
}
}
}
document.getElementById(“结果”).textContent=dispWord;
}
水平();
日志(randomWord,repLetHold);

如果我答对了问题:

randomWord.replace(new RegExp(repLetHold,'g')," - ")

替换所有出现的
repLetHold
(只要它不是由特殊的正则字符组成)

您的代码实际上看起来很好,主要问题是您在
for
循环之外声明了随机变量。这样做只会为整个循环生成一次。请尝试以下方法:

var dispWord;

var myWords = new Array("Spectrometer", "Bandwagonjghvyjh", "jvjyvyvilvyjlvyv", 
                        "fruitjjyvtyvjv", "seventctcvtv", "weathertfhtcthc", 
                        "undercfxdtfv"); // random letters to make words that have more than 10 letters

function level() { 
    for (i = 1; i <= 3; i++) //loop to replace three random characters with a hyphen 
    {
        var randHold = parseInt((Math.random() * 6) + 1);//code to randomly pick a word from the above array
        var randomWord = myWords[randHold]; //code to call the random word from the array
        var randLetHold = (Math.random() * randomWord.length);//code to randomly pick a character from the random word chosen
        var repLetHold = randomWord.charAt(randLetHold);//code to call the random character

        dispWord = randomWord.replace(repLetHold," - ");//code to replace a random character with a hyphen
        document.write(dispWord);//But all this does is display the word(with ONE hypenated character)three times.
    }

}
var-dispWord;
var myWords=新阵列(“光谱仪”、“波段JGHVYJH”、“JVJYVILVYJLVYV”,
“水果JJYVTYVJV”、“第七台CTCVTV”、“weathertfhtcthc”,
“undercfxdtfv”);//随机字母,使单词包含10个以上的字母
函数级别(){

对于(i=1;i这将用连字符替换整个单词。他想替换三个单独的字符。如果答案不正确,你应该做的就是投反对票。这会引起回答者对错误的注意,错误纠正后可以删除。我在投票前检查了你的JSFIDLE。你自己看看。你原来的表格had
var数组=randomWord.split(“”);
。这将导致整个单词被替换。不管怎样,它现在看起来是正确的,所以我将删除下一票。我不认为你会发现它是正确的,我知道你做了什么,它工作得很好,但我希望函数专门与循环一起工作。这意味着我希望替换函数/代码循环3次,所以我希望在世界中有三个字符我想让你大吃一惊,而不仅仅是一两个。但非常感谢你花时间回答我的问题。