Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheCoupe

macrumors member
Original poster
Mar 4, 2009
53
0
Northern Ireland
Hi all... I am a bit of a noob towards some javascript and need help regarding regExp. Basically, I have a single search field on my site, which when text is entered, splits each word into a separate search term and filters through multiple columns.

Everything works perfectly except for one flaw... the filter is searching for match of characters within each fields string rather than whole word matching or words starting with etc...

ie. on dates... if a user searches for 8th, this will also return 18th, or 20 will also return 2012.

I need to apply some form of expression to my filter that will ONLY apply to the ds1_date part of my search... such as: (?:^|\s)(str)(?:$|\s)... though I don't know how I should apply that to the following code:

Code:
function FilterData()
{
	var tf = document.getElementById("filterTF");
	if (!tf.value)
	{
		ds1.filter(null);
		return;
	}

	var regExpStr = tf.value;
	
	if (!document.getElementById("containsCB").checked)
		regExpStr = "^" + regExpStr;

	var regExp = new RegExp(regExpStr, "i");
	
	var values = tf.value.split(" ");
	var filterFunc = function(ds, row, rowNumber)
	{

		var numberWords = values.length;
		var counter = 0;
		
		for( var i = 0; i < numberWords; i++ ) {
			var regExp = new RegExp( values[i], "i");

			if (
			(row["ds1_date"] && row["ds1_date"].search(regExp) != -1)
			||
			(row["ds1_reg"] && row["ds1_reg"].search(regExp) != -1)
			||
			(row["ds1_desc"] && row["ds1_desc"].search(regExp) != -1)
			||
			(row["ds1_code"] && row["ds1_code"].search(regExp) != -1)
			) {
				counter++;
			}
		}
		if (numberWords != 0 && counter == numberWords)
		{
			return row;
		}
		return null;
	};
	ds1.filter(filterFunc);
}

Any help would be greatly appreciated.

Thanks
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.