The Little Guy Posted September 3, 2010 Share Posted September 3, 2010 In the following code, I am matching text against words in an array. It works, unless the input has a space in it. Why is that? if( (loc.toLowerCase().match(escape(rloc[l].toLowerCase())) != null || loc.toLowerCase().match(escape(l.toLowerCase())) != null) || (rloc[l].toLowerCase().match(escape(loc.toLowerCase())) != null || l.toLowerCase().match(escape(loc.toLowerCase())) != null) ){ Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 3, 2010 Share Posted September 3, 2010 The array contains regex correct? Since the match method evaluates a string against an expression. string.match(expression) Are you expressions correct? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 3, 2010 Author Share Posted September 3, 2010 its either a number or a string. example: 12345 or New York Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 3, 2010 Share Posted September 3, 2010 And are they expressions, consider the following example: var patterns = [/^New York$/i, /^12345$/]; var test_one = 'new york'; var test_two = 'new york city'; document.write(test_one.toString().match(patterns[0])); // will write new york document.write(test_two.toString().match(patterns[0])); // will write null Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 4, 2010 Author Share Posted September 4, 2010 basically what I am doing is like what facebook does. when you refine a search. and in the location field you type your city, and the auto complete shows up below the text box. Go here: http://www.newzstand.us/report in the location box type "apple valley" it should only display one result (or any others that say "apple valley") but doesnt. it stops filtering after I type the space. str is a string and when it goes to eval it get turned into an array (rloc). an array might look something like this (max of 100 items): rloc[55068] = 'Rosemount, MN'; rloc[55124] = 'Apple Valley, MN'; rloc[55401] = 'Minneapolis, MN'; loc is a string of what location the user has so far typed into the textbox. function showLocation(str, loc){ if(str != ''){ eval(str); } var suggest = document.getElementById('reportStoryLoacation'); var i = 0; var opt = ''; for(var l in rloc){ if( (loc.toLowerCase().match(escape(rloc[l].toLowerCase())) != null || loc.toLowerCase().match(escape(l.toLowerCase())) != null) || (rloc[l].toLowerCase().match(escape(loc.toLowerCase())) != null || l.toLowerCase().match(escape(loc.toLowerCase())) != null) ){ opt += '<a class="suggLink" href="javascript:void(0);" onclick="setStoryLocation(\''+rloc[l]+'\', '+l+');">'+rloc[l]+'</a>'; i++; }else{ rloc = Array(); return false; } if(i == 10) break; } if(opt.length < 1) opt += '<span class="suggLink" style="cursor: default;">No Matches Found...</span>'; suggest.innerHTML = opt; if(loc.length < 2){ suggest.style.display = 'none'; }else{ suggest.style.display = 'block'; } } Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 4, 2010 Share Posted September 4, 2010 Well, the only thing I can suggest is making sure they are strings with .toString(), and instead of using toLowerCase make your expressions case insensitive by using the i modifier. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 4, 2010 Author Share Posted September 4, 2010 how do you use an "i" modifier on a string? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 5, 2010 Share Posted September 5, 2010 You need to create a RegExp object out of your location, and use the i modifier on it. I took what you gave me and created an example, hopefully this is what you're after. <script type="text/javascript"> // Create an example user inputted string. var loc = 'Rose'; // Taken from your example. var rloc = []; rloc[55068] = 'Rosemount, MN'; rloc[55124] = 'Apple Valley, MN'; rloc[55401] = 'Minneapolis, MN'; // Convert the location to a string. loc = loc.toString(); // Create our regexp. regex = new RegExp(loc, 'i'); // Now run the for loop. for(var l in rloc){ // Now we can test them. if( rloc[l].match(regex) !== null || l.match(regex) !== null ){ // Write out the matches. document.write(rloc[l] + '<br />'); } } </script> Try copy and pasting that into a blank page and running it, change the loc to something else and test it out. I tried it and it seemed to be working. What I've done is created a RegExp object from the loc, firstly converting it to a string. Then you use the rloc variables as the test subject. Good luck man. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2010 Author Share Posted September 5, 2010 Thanks man! That worked! I had to do some mods to get it to work, but it works now thanks!!!!!!!!! Quote Link to comment Share on other sites More sharing options...
.josh Posted September 5, 2010 Share Posted September 5, 2010 btw the reason your original issue wasn't working with spaces was because you were escaping your value inside the .match() so it was trying to match a literal 'foo%20bar' not 'foo bar' Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2010 Author Share Posted September 5, 2010 oh... I though escape was adding slashes before special characters... my mistake Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.