make search case insnesitive

Discussion in 'JavaScript' started by gilgalbiblewheel, Nov 14, 2008.

  1. #1
    How do I add the case insensitive search?
    function getPhrases(){
    		var searchChapWords=document.getElementsByName('search_chap_words'); //grabs from the 1st column
    		var searchChapWords2=document.getElementsByName('search_chap_words2'); //grabs from the 2nd column
    		document.getElementById('keyword0').innerHTML = "";
    		document.getElementById('keyword1').innerHTML = "";
    		
    		var myOldString = "";
    		var myOldString2 = "";
    		for(i=0;i<searchChapWords.length;i++){
    			myOldString += searchChapWords[i].innerHTML + " "; //joins 1st column in one string
    		}
    		for(i=0;i<searchChapWords2.length;i++){
    			myOldString2 += searchChapWords2[i].innerHTML + " "; //joins 2nd column in one string
    		}
    	var myNewString = myOldString.replace(/[\:\;\?\.\,\(\)\']/gi, "");//to eliminate the punctuations
    	var myNewString2 = myOldString2.replace(/[\:\;\?\.\,\(\)\']/gi, "");//to eliminate the punctuations
    	
    	var mySplitString = myNewString.split(" ");
    	var mySplitString2 = myNewString2.split(" ");
    	
    	// Our common phrase goes here.
    	var common_phrase = "";
    	var phraseCollection =  new Array();
    	// Now go through a double-loop and match each common word.
    	//for(k=0; k<15; k++){
    		k=0;
    		phraseCollection[k] = "";
    		for(var i=0; i < mySplitString.length; i++){ //length of 1st array
    			if(i!=0){
    				//common_phrase += " ";
    				phraseCollection[k] += "";
    			}
    			for(var j=0; j < mySplitString2.length; j++){ //picks a word and runs through 2nd array
    				if(mySplitString[i] == mySplitString2[j]){ //when match found
    					phraseCollection[k] += mySplitString[i]+" "; //records word
    					// Move it forward to the next word in the phrase1.
    					i++; // goes to the next word
    				}
    			}
    			k++;//skips to the next array element
    			phraseCollection[k]="";
    		}
    	//}
    	function sortNumber(a,b){
    		return a - b;
    	}
    	var sortByLength = new Array();
    	
    	for(m=0; m<sortByLength.length; m++){
    		sortByLength[m] = phraseCollection[m].length;
    
    	}	
    	for(k=0; k<phraseCollection.length; k++){
    		if(phraseCollection[k].length> document.getElementById("number_words").value){
    			common_phrase += phraseCollection[k] + ",,";
    		}		
    	}
    	// Clean up the extra space at the end.
    	common_phrase = common_phrase.substr(0, common_phrase.length-1);
    	document.getElementById('keyword0').innerHTML=common_phrase;
    
    }
    Code (markup):

     
    gilgalbiblewheel, Nov 14, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    change
    
    var mySplitString = myNewString.split(" ");
    var mySplitString2 = myNewString2.split(" ");
    
    Code (markup):
    to

    
    var mySplitString = myNewString.toLowerCase().split(" ");
    var mySplitString2 = myNewString2.toLowerCase().split(" ");
    
    Code (markup):
    untested, but i think it should work.
     
    rene7705, Nov 15, 2008 IP