Jump to content

Matching Text Against an Array


The Little Guy

Recommended Posts

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)
){

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.