javascript - Case insensitive String search is not working -
please have @ followig code
<!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 count() { var listofwords, paragraph, listofwordsarray, paragrapharray; var wordcounter=0; listofwords = document.getelementbyid("wordslist").value; //split words listofwordsarray = listofwords.split("\n"); //convert entire word list upper case for(var i=0;i<listofwordsarray.length;i++) { listofwordsarray[i] = listofwordsarray[i].touppercase(); } //get paragrah text paragraph = document.getelementbyid("paragraph").value; paragrapharray = paragraph.split(" "); //convert entire paragraph upper case for(var i=0; i<paragrapharray.length; i++) { paragrapharray[i] = paragrapharray[i].touppercase(); } //check whether paragraph contains words in list for(var i=0; i<listofwordsarray.length; i++) { /* if(paragraph.contains(listofwords[i])) { wordcounter++; }*/ re = new regexp("\\b"+listofwordsarray[i]+"\\b"); if(paragraph.match(re)) { wordcounter++; } } window.alert("number of contains: "+wordcounter); } </script> </head> <body> <center> <p> enter word list here </p> <br /> <textarea id="wordslist" cols="100" rows="10"></textarea> <br /> <p>enter paragraph here</p> <textarea id="paragraph" cols="100" rows="15"></textarea> <br /> <br /> <button id="btn1" onclick="count()">calculate percentage</button> </center> </body> </html> here, trying counting how number of words in paragraph included in wordlist. words in wordlist separated new line.
however need check case insensitive. example, there should no difference between 'count' , 'count' , 'count'.
but here, getting answer 0. doing wrong here? please help.
update
i tried following function, provided user 'kolink'. giving different answers in different runs. in first few runs correct, starts provide wrong answers! may javascript static variables?
you preparing paragraph's words in paragrapharray never use it.
i suggest this:
var words = document.getelementbyid('wordslist').value.split(/\r?\n/), l = words.length, i, total = 0, para = document.getelementbyid('paragraph').value; for( i=0; i<l; i++) if( para.match(new regexp("\\b"+words[i]+"\\b","i"))) total++; alert("total: "+total);
Comments
Post a Comment