次のコードを見てください
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
//Function to Trim
String.prototype.trim = function()
{
return this.replace(/^\s+|\s+$/g,"");
};
//Function to remove punctuation
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
function count()
{
var listOfWords, paragraph, listOfWordsArray, paragraphArray;
var wordCounter=0;
listOfWords = document.getElementById("wordsList").value.trim();
listOfWords = listOfWords.toUpperCase();
//Split the words
listOfWordsArray = listOfWords.split(/\r?\n/);
//Get the paragrah text
paragraph = document.getElementById("paragraph").value.trim();;
paragraph = paragraph.toUpperCase();
//Filter all the punctuations
replaceAll("\"","",paragraph);
replaceAll("[","",paragraph);
replaceAll("]","",paragraph);
replaceAll("{","",paragraph);
replaceAll("}","",paragraph);
replaceAll("(","",paragraph);
replaceAll(")","",paragraph);
replaceAll("<","",paragraph);
replaceAll(">","",paragraph);
replaceAll(":","",paragraph);
replaceAll(",","",paragraph);
replaceAll("-","",paragraph);
replaceAll("...","",paragraph);
replaceAll("!","",paragraph);
replaceAll("<<","",paragraph);
replaceAll(">>","",paragraph);
replaceAll("","",paragraph);
replaceAll(".","",paragraph);
replaceAll("?","",paragraph);
replaceAll("/","",paragraph);
replaceAll("\\","",paragraph);
paragraphArray = paragraph.split(" ");
//check whether paragraph contains words in list
for(var i=0; i<paragraphArray.length; i++)
{
//re = new RegExp("\\b"+paragraphArray[i]+"\\b","i");
if (listOfWordsArray.indexOf(paragraphArray[i]) >= 0)
{
}
else
{
wordCounter++;
}
}
var average =0;
average = (wordCounter/paragraphArray.length)*100;
average = 100-average;
average = Math.round(average*100)/100;
window.alert("Number of Words: "+paragraphArray.length+ "\n"+ "Number of Unmatched words: "+wordCounter+ "\n"+ "Percentage "+average+"%" );
}
</script>
</head>
<body>
<center>
<p> Enter your Word List here </p>
<br />
<textarea id="wordsList" cols="100" rows="10"></textarea>
<br />
<p>Enter your paragraph here</p>
<textarea id="paragraph" cols="100" rows="15"></textarea>
<br />
<br />
<button id="btn1" onclick="count()">Calculate Percentage</button>
</center>
</body>
</html>
ここでは、テキストからすべての句読点を削除しようとしています。しかし、私のコードでは出力が得られません。Web スクリプト言語に関する知識が不足しているため、解決策が見つかりません。テキストから句読点を削除するにはどうすればよいですか? ここで何が間違っていますか?
アップデート
Rahul の回答によると、次のようにコードを編集しましたが、残念ながらまだうまくいきません。最初の句読点だけでなく、テキスト全体からすべての句読点を削除したい。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
//Function to Trim
String.prototype.trim = function()
{
return this.replace(/^\s+|\s+$/g,"");
};
//Function to remove punctuation
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
function count()
{
var listOfWords, paragraph, listOfWordsArray, paragraphArray;
var wordCounter=0;
listOfWords = document.getElementById("wordsList").value.trim();
listOfWords = listOfWords.toUpperCase();
//Split the words
listOfWordsArray = listOfWords.split(/\r?\n/);
//Get the paragrah text
paragraph = document.getElementById("paragraph").value.trim();;
paragraph = paragraph.toUpperCase();
//Filter all the punctuations
var newstring= paragraph.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"");
var finalString = newstring.replace(/\s{2,}/g," ");
paragraphArray = finalString.split(" ");
//check whether paragraph contains words in list
for(var i=0; i<paragraphArray.length; i++)
{
//re = new RegExp("\\b"+paragraphArray[i]+"\\b","i");
if (listOfWordsArray.indexOf(paragraphArray[i]) >= 0)
{
}
else
{
wordCounter++;
}
}
var average =0;
average = (wordCounter/paragraphArray.length)*100;
average = 100-average;
average = Math.round(average*100)/100;
window.alert("Number of Words: "+paragraphArray.length+ "\n"+ "Number of Unmatched words: "+wordCounter+ "\n"+ "Percentage "+average+"%" );
}
</script>
</head>
<body>
<center>
<p> Enter your Word List here </p>
<br />
<textarea id="wordsList" cols="100" rows="10"></textarea>
<br />
<p>Enter your paragraph here</p>
<textarea id="paragraph" cols="100" rows="15"></textarea>
<br />
<br />
<button id="btn1" onclick="count()">Calculate Percentage</button>
</center>
</body>
</html>