www.webdeveloper.com
Results 1 to 5 of 5

Thread: String.search method is frustrating... Can't make case-insensitive searches work.

  1. #1
    Join Date
    Nov 2005
    Location
    India
    Posts
    336

    String.search method is frustrating... Can't make case-insensitive searches work.

    PHP Code:
    <html>
        <
    head>
            <
    title>
                
    Javascript Testing
            
    </title>
        </
    head>
        
        <
    body>
            
            <
    form name="myForm">
                <
    table>
                    <
    tr><td>Name:</td><td><input type="text" name="nameget" onkeyup="suggest();"></input></td></tr>
                    <
    tr><td>Suggestion:</td><td><div id="suggestion"></div></td></tr>
                </
    table>
                
            </
    form>
            
            <
    script type="text/javascript">
                var 
    dictionary;
        
                function 
    pullData() // Pull the words from the file to an array named dictionary.
                
    {
                    var 
    xmlHttp;
                    try
                    {
                        
    // Firefox, Opera 8.0+, Safari
                        
    xmlHttp = new XMLHttpRequest();
                    }
                    catch (
    e)
                    {
                        
    // Internet Explorer
                        
    try
                        {
                            
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                        }
                        catch (
    e)
                        {
                            try
                            {
                                
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            catch (
    e)
                            {
                                return 
    false;
                            }
                        }
                    }
                
                    var 
    getsrc "dictionary.txt"// File to pull words from
            
                    
    xmlHttp.open("GET",getsrc,false); // Open the file
                    
    xmlHttp.send(null);

                    var 
    dictresponse xmlHttp.responseText// Get response
                    
    dictionary dictresponse.split("\n");   // Articulate - newline is separator
                
    }
                
                
    pullData(); // Execute
                
                
    function suggest() // Suggests names based on user input
                
    {
                    
    document.getElementById('suggestion').innerHTML ""// Clear the div
                    
                    
    for (i=0;i<dictionary.length;i++) // Loop through each value in the dictionary array
                    
    {
                        var 
    match document.myForm.nameget.value;  // Pass to variable for regex evaluation
                        
    match "^" match// Must be at the beginning
                        
    if ((dictionary[i].search(match) != -1) && myForm.nameget.value != ''// If a match is found...
                        
    {
                            
    document.getElementById('suggestion').innerHTML += dictionary[i] + "<br>"// ... display it in the "suggestion" div
                        
    }
                    }
                }

            
    </script>
        </body>
    </html> 
    My problem is in the suggest() function. "(dictionary[i].search(match)" to be specific. How can I make this search case-insensitive? It seems that I can't use "search(/match/i)" because match is a variable.

    I've tried applying various examples on the web to no avail. Any suggestions?

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    It seems that I can't use "search(/match/i)" because match is a variable.
    Oh, but you can use Regular Expressions and their flags, except that you need the RegExp() object for the variables. And better avoid using match as the name of the variable. It is a javascript reserved word.
    Code:
    var matchW= new RegExp(document.myForm.nameget.value,'gi');
    if ((dictionary[i].search(matchW)....
    Last edited by Kor; 06-04-2008 at 12:29 PM.

  3. #3
    Join Date
    Nov 2005
    Location
    India
    Posts
    336
    Quote Originally Posted by Kor
    Oh, but you can use Regular Expressions and their flags, except that you need the RegExp() object for the variables. And better avoid using match as the name of the variable. It is a javascript reserved word.
    Code:
    var matchW= new RegExp(document.myForm.nameget.value,'gi');
    if ((dictionary[i].search(matchW)....
    Hurray. Works perfectly.

    Thanks for the warning about "match."

  4. #4
    Join Date
    Nov 2005
    Location
    India
    Posts
    336
    Here's what I was trying to do, by the way: http://76.74.137.138/nametest.html (Type in your name, and see if it shows up!)

  5. #5
    Join Date
    Aug 2007
    Posts
    3,767
    Well implemented. You're missing my first name alright, Declan.
    Great wit and madness are near allied, and fine a line their bounds divide.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles