5

JavaScript では、文中の正規表現をランダムに生成されたその正規表現の一致に自動的に置き換えることは可能ですか? このアプローチを使用して、次のように正規表現のリストを使用して文を自動的に言い換えようとしています。

replaceWithRandomFromRegexes("You aren't a crackpot! You're a prodigy!", ["(genius|prodigy)", "(freak|loony|crackpot|crank|crazy)", "(You're |You are |Thou art )", "(aren't|ain't|are not)"])

ここでは、入力文字列内の各正規表現の各一致を、ランダムに生成された正規表現の一致に置き換える必要があります。

function replaceWithRandomFromRegexes(theString, theRegexes){
    //For each regex in theRegexes, replace the first match of the regex in the string with a randomly generated match of that regex.
}
4

2 に答える 2

5

はい、これは確かに可能です。replaceWithRandomFromRegexesこのタスクを実行するために呼び出される関数を作成しました。

http://jsfiddle.net/KZyZW/2/

for(var i = 0; i < 10; i++){
document.body.innerHTML += (replaceWithRandomFromRegexes("You aren't a crackpot! You're a prodigy!", ["(genius|prodigy)", "(freak|loony|crackpot|crank|crazy)", "(You're |You are |Thou art )", "(aren't|ain't|are not)"]))+"<br/>";
}

function replaceWithRandomFromRegexes(theString, theRegexes) {
    //alert(theRegexes);
    for (var i = 0; i < theRegexes.length; i++) {
        theString = globalReplaceWithRandomFromRegex(theString, theRegexes[i]);
        //alert(theRegexes[i]);
    }
    //alert("All the regexes: " + theRegexes);
    return theString;
}

function globalReplaceWithRandomFromRegex(theString, theRegexString) {
    var theRegex = new RegExp(theRegexString, "gi");
    //replace all matches of theRegex with '<thing to replace>'
    theString = theString.replace(theRegex, "<thing to replace>")


    //replace the first match of '<thing>'

    while (theString.indexOf("<thing to replace>") != -1) {
        theString = theString.replace("<thing to replace>", getRandomStringFromNestedParentheses(theRegexString));
    }

    //alert(theString);
    return theString;
}

function getRandomStringFromNestedParentheses(theString) {
    while (theString.indexOf("(") != -1) {
        theString = replaceInFirstParentheses(theString);
    }
    return theString;
}

function replaceInFirstParentheses(theString) {
    //find the index of the first parentheses
    var parenthesesIndex = theString.indexOf("(");
    var randomString = getRandomStringInsideParentheses(theString, parenthesesIndex);
    //alert(randomString);

    //theString = theString.replace();

    //find the string to replace
    var stringToReplace = theString.substring(parenthesesIndex, getCorrespondingParenthesesIndex(theString, parenthesesIndex) + 1);
    //alert(stringToReplace);
    theString = theString.replace(stringToReplace, randomString);
    //alert(theString);
    return theString;
}

function getRandomStringInsideParentheses(string, parenthesesIndex) {
    var stringArray = getStringsInsideParentheses(string, parenthesesIndex)
    //find out how to pick random in a range
    //pick something random from the array declared above
    //var theMin = 0;
    //var theMax = stringArray.length-1;
    var randomNumber = Math.floor(Math.random() * stringArray.length);
    return stringArray[randomNumber];
}

function getStringsInsideParentheses(string, parenthesesIndex) {
    //alert("calling function getStringsInsideParentheses");
    var theString = getStringFromParentheses(string, parenthesesIndex);
    for (var i = 0; i < theString.length; i++) {
        var theParenthesesNum = getParenthesesNum(theString, i);
        if (theString[i] == '|') {
            //alert("Parentheses num: " + theParenthesesNum);
            if (theParenthesesNum == 0) {
                theString = theString.substring(0, i) + "|" + theString.substring(i, theString.length);
                i++;
            }
        }
    }
    //alert(theString);
    return theString.split("||")
}

function getStringFromParentheses(theString, parenthesesIndex) {
    return theString.substring(parenthesesIndex + 1, getCorrespondingParenthesesIndex(theString, parenthesesIndex))
}

function getCorrespondingParenthesesIndex(theString, openingParenthesesIndex) {
    if (!parenthesesAreMatching(theString)) {
        writeMessage("Error: The parentheses do not match!");
        return false;
    }
    if (theString.charAt(openingParenthesesIndex) != "(") {
        writeMessage("Error: The index must be an opening parentheses!");
        return false;
    }
    var num = 0;
    for (var i = openingParenthesesIndex; i < theString.length; i++) {
        if (theString.charAt(i) == "(") {
            num++;
        }
        if (theString.charAt(i) == ")") {
            num--;
        }
        if (num == 0) {
            return i;
        }
    }
    writeMessage("Error: The parentheses do not match!");
    return false;
}

function parenthesesAreMatching(theString) {
    var num = 0;
    for (var i = 0; i < theString.length; i++) {
        if (theString.charAt(i) == "(") {
            num++;
        }
        if (theString.charAt(i) == ")") {
            num--;
        }
    }
    if (num == 0) {
        return i;
    } else {
        return false;
    }
}

function getParenthesesNum(theString, index) {
    //this should be based on parenthesesAreMatching, but stopping at index
    var num = 0;
    for (var i = 0; i < index; i++) {
        if (theString.charAt(i) == "(") {
            num++;
        }
        if (theString.charAt(i) == ")") {
            num--;
        }
    }
    return num;
}

元の文はYou aren't a crackpot! You're a prodigy!、このスクリプトによって生成された、この文の自動的に言い換えられたバージョンです。

You are not a freak! Thou art a genius!
You aren't a crackpot! You're a prodigy!
You ain't a crackpot! You are a genius!
You ain't a freak! You're a prodigy!
You are not a crackpot! You are a prodigy!
You are not a loony! You're a prodigy!
You are not a loony! You are a genius!
You are not a loony! You are a prodigy!
You ain't a crackpot! Thou art a prodigy!
You are not a loony! Thou art a prodigy!
于 2013-05-18T17:48:50.180 に答える