javascript - 替换标点符号和双引号

标签 javascript html web dreamweaver

请看下面的代码

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

最佳答案

也许你可以尝试这样:-

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 .

关于javascript - 替换标点符号和双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18676818/

相关文章:

javascript - for var index in 与 forEach

javascript - selection.modify() 是否有 IE 替代品?

html - 使用 .X.Y 类型选择器的属性奇怪行为后的 CSS

java - Jsoup:需要帮助从元标记中提取内容值

php - fetch_assoc 的排序

javascript - 如何根据样式获取特定元素javascript

javascript - Selectize.js 不工作

php - HTML 选择中的 MySQL 列

javascript - jQuery on() 或 live

javascript - 渐进式 Web 应用程序在性能方面与普通 Web 应用程序有何不同