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
https://forums.phpfreaks.com/topic/212412-matching-text-against-an-array/
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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.