views:

270

answers:

4

Hi,
I have a combo box and input box. If I enter any letter in input box then all the words or sentence that match with that letter should display in the combo box assuming that a list of words or sentence contain in the list.

ex1:)  input box: a
       combo box : America 
                   Australia
                   joy is in Austria

For this example I want JavaScript regex patterns. Anybody give me suggestion.

Thanks
Usman.sk

A: 

Have the word put into a word boundary as you type. Just replace the [word here] part below with your typing from the textbox.

The expression would be:

\b[word here]\b

Also, here's a good regex tester.

ryanulit
+2  A: 

Not sure I understand exactly what you're asking for, but I think you don't want regex, you just want jQuery:

Like this:

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <script>
        $j = jQuery.noConflict();

        $j(document).ready(init);

        function init()
        {
            $j('#MyEditBox').change( filterCombo ).keyup( filterCombo );
        }

        function filterCombo()
        {
            var InputText = $j(this).val();
            $j('#MyCombo option:not(:contains('+ InputText +'))').hide();
            $j('#MyCombo option:contains('+ InputText +')').show();
        }
    </script>
</head>
<body>
    <input type="text" id="MyEditBox" />
    <select multiple id="MyCombo">
        <option>America
        <option>Australia
        <option>Something else
        <option>Joy is in Austria
        <option>Wibble
    </select>
</html>


Update: This one will only match whole words:

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <script>
        $j = jQuery.noConflict();


        $j(document).ready(init);


        function init()
        {
            $j('#MyEditBox').change( filterCombo ).keyup( filterCombo );
        }


        function filterCombo()
        {
            var InputText = $j(this).val();

      if (InputText.length > 0)
      {
       $j('#MyCombo option')
        .hide()
        .filter(function() { return hasWholeWord( $j(this).val() , InputText ) })
        .show()
        ;
      }
      else
      {
       $j('#MyCombo option').show();
      }
        }


        function hasWholeWord( Text , Word )
        {
            return (new RegExp('\\b'+Word+'\\b','i').test( Text ));
        }
    </script>
</head>
<body>
    <input type="text" id="MyEditBox" value="" />
    <select multiple id="MyCombo">
        <option>America
        <option>Australia
        <option>Something else
        <option>Joy is in Austria
        <option>Wibble
    </select>
</html>
Peter Boughton
why was this given an negative vote?It seems like a valid solution to me
ErsatzRyan
filterCombo() where we have declare.i mean inside the ready function or outside
usman
Well, as per the "something along the lines of", it was intended as a general example, not a precise copy-paste example. But anyhow, I've added a ready/init function in.
Peter Boughton
Actually, here we go - a full HTML example. :)
Peter Boughton
it is working for me ,but i have some condition here. when i enter 'E' in the input box i am getting America, Somethig else, Wibble. but actually it should not show any result.search should be for entire word like joy not jo . Any idea please
usman
Ok, I've updated it now to only show results when a whole word matches. Of course, this now uses the word-boundary regex. :)
Peter Boughton
A: 

I think it depends a little on where you want to match from. In your example the letter 'a' matches both at the start and the end of "America", is that a sufficient match?

Depending on how you want to filter your data you could use jLinq (Disclaimer: My project :) )

Depending on where you want to match from you may want to use the '\b' option to try and limit where an acceptable match comes from...

\ba - Match 'a' from the start

a\b - match 'a' from the end

Additionally, if you're interested in the value of the match, you could use something like the following...

(?<1>\ba\w+) - This is a little harder to read, but this matches anything that starts with an 'a', but also returns the entire word as a group within the match.

Hugoware
sorry please tell me , (?<1>\ba\w+) where i have to add this ?
usman
something along the lines of ... str.match(/(?<1>\ba\w+)/gi) when looping through to determine which values you want to keep. If that doesn't make sense let me know and I'll post more in the answer
Hugoware
Yep, this works better than my answer. Also, I believe it would only match to a lowercase a and not an uppercase A, so you may want to use like .ToUpper(), or whatever it is in Javascript, and then match to (?<1>\bA\w+).
ryanulit
Well sorta, I left the case matching up to the actual .match() function with the /gi flag (global and ignore case)
Hugoware
A: 

For a simple substring match, you don't need regex, just a plain myString.indexOf(inputString) should suffice. For case-insensitive you can do myString.toUpperCase().indexOf(inputString.toUpperCase()).

jiggy