1

我目前正在尝试在 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.
        }

    }
4

4 回答 4

1

对于要在单词中连字符的 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);

jsfiddle上

于 2013-04-20T15:20:14.680 回答
1

您的代码实际上看起来不错,主要问题是您在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.
    }

}
于 2013-04-20T15:34:22.983 回答
0

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";

请记住,某些字符必须在正则表达式中进行转义。

于 2013-04-20T15:15:04.757 回答
0

http://jsfiddle.net/zt8mp/

如果我的问题是正确的:

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

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

于 2013-04-20T15:20:27.860 回答