1

请看下面的代码

<!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>

在这里,我试图从文本中删除所有标点符号。但是我的代码没有输出。我在网络脚本语言方面缺乏知识,所以我找不到解决方案。如何从文本中删除标点符号?我在这里做错了什么?

更新

根据 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>
4

1 回答 1

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

于 2013-09-07T19:07:53.843 回答