Shoutbox

how to use special characters in a switch - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: how to use special characters in a switch (/showthread.php?tid=81506)

how to use special characters in a switch by koen on 02-07-2008 at 09:35 AM

hi,
i have a question about using special characters in a switch. I have searched in the Scripting documentation,internet, this forum,... but i can't make it work.

I have a switch that works:
switch(Message) {
             case "questions": ChatWnd.SendMessage("sentence");
             break;
                ...

but now i want to compare 'Message' to a string with for example 'the'  in it.

like this: if Message = 'the best question ever'
how can i make the switch trigger this by the word 'question'?

switch(Message){
           case "^.question.$": ....                something like this doesn't work :s

i have read in the documentation that '.' matches any single character, '^' is the beginning of a string, '$' is the end of a string,...

but i can't make it work...


RE: how to use special characters in a switch by Matti on 02-07-2008 at 10:52 AM

Well, you can make a regular expression which does this, but you can't use that in a switch...case block.

code:
var reQuestion = /(^|\s)question(\s|$)/si; //Create a regular expression which contains the word "question".
if(reQuestion.test(Message)) { //Test the message on the regular expression
   ChatWnd.SendMessage("Target valid!");
} else {
   ChatWnd.SendMessage("Aborting, couldn't see target.");
}
Note that I made this regular expression so it'll match the word "question" only. That means, it won't match "questions". If you want it to match any occurrence of "question", it gets much simpler. Then, you can simply use this instead of the original first line:
code:
var reQuestion = /question/i; //Create a regular expression which contains "question".

Note: In all these regular expressions, the "i" modifier was placed so they're case-insensitive. This is interesting if you capitalize the word for example. If you want it to be case-sensitive, just remove the "i" at the end. ;)
RE: how to use special characters in a switch by koen on 02-07-2008 at 11:01 AM

oh thx! now it works :)


RE: how to use special characters in a switch by markee on 02-07-2008 at 01:13 PM

quote:
Originally posted by Mattike
code:
var reQuestion = /(^|\s)question(\s|$)/si;


I think you mean the following expression....

code:
var reQuestion = /\bquestion\b/i;

\b just denotes the end of a word (beginning or end), if you were to have the lines of a commar or questionmark after the word "question" then yours wouldn't fire.  Also the s modifier doesn't exist in JScript ;)
RE: how to use special characters in a switch by Spunky on 02-07-2008 at 10:00 PM

quote:
Originally posted by markee
Also the s modifier doesn't exist in JScript

I usedit fine and my script didn't work correctly without it ^o)
RE: how to use special characters in a switch by markee on 02-08-2008 at 05:28 AM

I couldn't find any documentation online about it being part of JScript.  However it is used for making the likes of "." match ANY character including \r and \n (both of these are not able to be matched by "." normally).

I wonder if we can also use the x modifier.... (I will test it out later)


RE: how to use special characters in a switch by Matti on 02-08-2008 at 08:22 AM

Bah, okay then markee. I simply did some research on regular-expressions.info and I didn't scroll down enough. :P Thanks for correcting me.


RE: how to use special characters in a switch by Spunky on 02-08-2008 at 05:42 PM

quote:
Originally posted by markee
I couldn't find any documentation online about it being part of JScript

http://www.javascriptkit.com/javatutors/redev2.shtml#
http://authors.aspalliance.com/wsk/aboutregularexpression.asp

Two pages from Google I found ^o)
RE: how to use special characters in a switch by markee on 02-09-2008 at 02:13 AM

quote:
Originally posted by SpunkyLoveMuff
quote:
Originally posted by markee
I couldn't find any documentation online about it being part of JScript

http://www.javascriptkit.com/javatutors/redev2.shtml#
http://authors.aspalliance.com/wsk/aboutregularexpression.asp

Two pages from Google I found ^o)
I was talking specifically about the smodifier, your first link has g,iand m, and the second only talks about g and i.
RE: how to use special characters in a switch by Spunky on 02-09-2008 at 11:10 AM

Thats my fault then. I only saw the \s and thought it was that :$