-1

.search JS method seems to want a string literal not a variable…

for (app in Servers)
{
    the_name = Servers [app]["Name"];
    the_app = Servers [app]["App Name"];
    search_str = "/"+Name+"/";
    hit = the_app.search(search_str);
    if (the_name == Name || the_app == "Foo Bar" )
    {
        result.Index = app;  
    }
}

I want to generate a hit for a set of Apps that have a common Word for e.g. Name = "Skittles" will hit on Apps where app_name == "Skittles 1.0"; "Skittles 1.5"; "Skittles 3.2 — The Deluxe Edition 3.2"; etc etc

Name is a user definable variable. Servers is an object made available to me that lists video servers I can stream from running on my machine, each item in Servers will correspond to a server and have a "Name" item and a "App Name" item in it's object. (It's Syphon thing this is not an HTML/web context).

Should I look at using a different method to construct the regexp?

wide_eyed_pupil
  • 2,631
  • 7
  • 20
  • 28

2 Answers2

1

Why RegExp? Use indexOf, it's the counterpart of search but for strings.

Update: Then use new RegExp(Name) to turn the string into a regular expression.

Update: Now that you updated your question, indexOf with "Skittles" will still work with all of your app_names.

for (app in Servers)
{
    the_name = Servers [app]["Name"];
    the_app = Servers [app]["App Name"];

    rx = new RegExp(Name);
    hit = the_app.search(rx);

    if (the_name == Name || the_app == "Foo Bar" )
    {
        result.Index = app;  
    }
}
Prinzhorn
  • 20,711
  • 6
  • 54
  • 62
  • Sorry should have mentioned I want to test for a set of values not just one exact hit. Will change the post to reflect that. Good point on indexOf though, I've never used it, thanks. – wide_eyed_pupil Aug 14 '12 at 07:40
  • So foo = new RegExp(Name); hit = the_app.search(foo) kinda thing? – wide_eyed_pupil Aug 14 '12 at 07:45
  • Hey thanks. I was tripping myself up with the Name variable being used for both tests when in fact the "App-Name" and "Name" are different so require different inputs to test against ("Name" has a hyphen in it so there's no spaces, where as "App Name" is what appears in OS X Finder). SOrted at last. Thanks for your patience @Prinzhorn – wide_eyed_pupil Aug 14 '12 at 08:08
  • Noted: …indexOf … will still work with all of your app_names. – wide_eyed_pupil Aug 14 '12 at 08:13
1

Use "RegExp" function to create your pattern. See here

You just create a pattern variable like this:

var pattern = new RegExp( --pattern-- , --modifiers--);

Cheers

Daniel Rivas
  • 236
  • 4
  • 13