1

我很难弄清楚如何通过 textarea 搜索并找到句点,在找到我需要随机决定是否应该在句点之后添加“arrr”的句点之后,这应该是 50/50 的机会里面印着的字。请帮帮我!!!这是我到目前为止所做的事情,有点混乱 document.getElementById("translate").onclick = function changed() {

var outpara = document.getElementById("out")
var paragraph = document.getElementById("input").value;
paragraph = paragraph.toLowerCase();
while(paragraph.indexOf("hello")!== -1) 
{ 
paragraph = paragraph.replace("hello", "Ahoy"); 
}
while(paragraph.indexOf("excuse me")!== -1) 
{
paragraph = paragraph.replace("excuse me", "arrr"); 
}
while(paragraph.indexOf("sir")!== -1) 
{
paragraph = paragraph.replace("sir", "matey"); 
}
while(paragraph.indexOf("madam")!== -1) 
{
paragraph = paragraph.replace("madam", "proud beauty"); 
}
while(paragraph.indexOf("officer")!== -1) 
{
paragraph = paragraph.replace("officer", "foul blaggart"); 
}
while(paragraph.indexOf("where is")!== -1) 
{
paragraph = paragraph.replace("where is", "whar be"); 
}
while(paragraph.indexOf("can you help me find")!== -1) 
{
paragraph = paragraph.replace("can you help me find", "know ye"); 
}
while(paragraph.indexOf("is that")!== -1) 
{
paragraph = paragraph.replace("is that", "be that"); 
}
while(paragraph.indexOf("the")!== -1) 
{
paragraph = paragraph.replace("the", "th'"); 
}
while(paragraph.indexOf("my")!== -1) 
{
paragraph = paragraph.replace("my", "me"); 
}
while(paragraph.indexOf("your")!== -1) 
{
paragraph = paragraph.replace("your", "yer"); 
}   
while(paragraph.indexOf("restroom")!== -1) 
{
paragraph = paragraph.replace("restroom", "head"); 
}   
while(paragraph.indexOf("restaurant")!== -1) 
{
paragraph = paragraph.replace("restaurant", "galley"); 
}
while(paragraph.indexOf("hotel")!== -1) 
{
paragraph = paragraph.replace("hotel", "fleabag inn"); 
}
var x = paragraph.split(" ");
var repl= [Math.floor (Math.random()* 2)]
    if(repl === 0 || repl === 1)
        paragraph.replace(".", ". arrrr")
    else
        paragraph.replace(".", ".")
var readySentance=[];
while(x.indexOf(".")!==-1)
{
    for(var i = 0; i < paragraph.length; ++i)
        {
        x.push(". Arrrr");
        }
}

我的 HTML 看起来像这样

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Pirate Translator</title>
<!--<link rel = "stylesheet" href="css/normalize.css" />
<link rel = "stylesheet" href="css/styles.css" />-->
</head>
<body>
<h1> Land Lovin' Lady's Pirate Translator</h1>
<p> Simply click on the buttons to translate words and/or phrases from English to  pirate talk</p><hr />
<form name = "pirateForm">

<div>   
<textarea id="input"></textarea>
<textarea id="out" readonly></textarea>
<input onclick= "changed();" type="button" id ="translate" value="translate"/>
<input onclick="clearArea2()" type="button" id="clear2" value= "clear"/>
<script src="pirate4.js"></script><br>

</form>
</div>
</body>
</html>
4

1 回答 1

2

g在替换中使用正则表达式(以便您可以指定全局标志)和回调:

paragraph = paragraph.replace(/\./g, function(){ return Math.random() < 0.5 ? ". arrrr" : "." });

arrrr由于这是用一个命令替换所有出现的事件,因此无论您必须进行多少次尝试才能通过随机性,您都不会陷入一个循环,该循环将在每个句点之后放置一个。

以同样的方式,您可以在所有替换中使用正则表达式,这样您就不需要将它们放在循环中。这将替换所有出现的hello

paragraph = paragraph.replace(/hello/g, "Ahoy");
于 2013-03-18T02:05:58.583 回答