请查看以下代码
<!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>
答案 0 :(得分:1)
可能你可以尝试这样: -
var s = "Your%^%*^%^&*^% string '"+"'which*^&^&( consists of punctuation";
var st = s.replace(/["']/g, "")
var newstring= st.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`"~()]/g,"");
var finalString = newstring.replace(/\s{2,}/g," ");
alert(finalString);
以上代码将替换字符串s
上面的代码工作正常。请查看JSFIDDLE。