PDA

View Full Version : Search array using wildcard


km176351
11-17-2005, 03:16 PM
Hi, I have an array set up in the following format:
w["about"]= "12345 "
w["belong"]="6789"
w["category']="12345"

It bascically acts as a search engine used in the following way:
if(w["category"] // does the word category exist in the array

My question is how I could possibly incorporate a wildcard feature thus if a user tried to search for "cat", although it doesn't exist as a word in itself, it would pick up category (something like search for cat****)

Thanks for your time,

RysChwith
11-17-2005, 04:29 PM
Are you familiar at all with regular expressions in JavaScript? You'll likely need to use them for this.

Rys

km176351
11-17-2005, 05:40 PM
Thanks, but I'm afraid I haven't used them before. I've found numerous sites about regular expressions though and the * option looks most appropiate although I can't seem to find how or if it is possible to use a string containing this wildcard regular expression as the reference

RysChwith
11-18-2005, 08:18 AM
It's not that easy, I'm afraid. What you're going to have to do is take in the user input, convert it to a regular expression, then use that expression to search through the array item by item. I'm not sure you can search array keys, though, so you may have to change the array.

Rys