JScript AddTrigger with Regex

Posted by Mat on Fri 16 May 2014 04:49 PM — 3 posts, 20,183 views.

#0
I'm having some trouble with regex on an addtrigger in JScript. I've done this successfully in VBScript. I've also added the trigger successfully without the regex, but once the regex is added EVERY single line makes it trigger.
I don't understand what I'm doing wrong and why it's only in JScript. Mushclient v4.84

world.addtrigger("detect_exits", "[You can move: .*]", "", 33, 14, 0, "", "ExampleTrigger");
}
function ExampleTrigger (thename, theoutput, wildcardsVB)
{
wildcards = VBArray(wildcardsVB).toArray();

world.note ("Trigger " + thename + " fired.");
world.note ("Matching line was: " + theoutput);

for (i = 0; i < 10; i++)
if (wildcards != "")
world.note ("Wildcard " + i + " = " + wildcards );

}

Let me know if you can help in any way,
Thanks
Australia Forum Administrator #1

[You can move: .*]


The square brackets make a "set".

Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.


This matches any of those characters in the set, not just the sequence of them. That would match any line containing "Y", "o", "u" and so on, including the space character. Since most lines will have spaces in them, it will match most lines.

For example, this matches any line with a space in it:


[ ]


If the square brackets are part of the line "escape" them like this:


\[You can move: .*\]


In Jscript you might have to put the backslashes twice because a backslash means something to Jscript (eg. \n being a newline) so you need to double them to actually have the backslashes hit the regexp parser. eg.


 world.addtrigger("detect_exits", "\\[You can move: .*\\]", "", 33, 14, 0, "", "ExampleTrigger");
Amended on Sat 17 May 2014 05:16 AM by Nick Gammon
#2
You were right on the money Nick!
I did have the single backslashes, they just weren't displayed when I posted. The double backslashes did the trick.

Thanks very much, wasn't able to figure that one out on my own.