quote:
Originally posted by CookieRevised
almost, since that regular expression does not work properly.
What you need to take in account:
- a command always starts with only 1 slash "/" (not two, or more)
^^ your reg exp fails here since you don't exclude second or more slashes.
- after the slash there can not be a spacing character
^^ your reg exp fails here since you define the exclussion list to be 0 or more times ("*") when it should be 1 or more times ("+").
PS: a space (ascii 32) is also a spacing character ("\s") so you don't need to define that seperatly in the exclussion list.
- commands should be case insensitive, but parameters are not!
^^ your reg exp fails here since you don't lowercase the command. Yet you use the case insensitive marker. But this does not do anything, since there are no characters in the regular expression. That marker is for searching on all cases, not to convert cases.
- after the command there can be many spacing characters. In many cases (if not all), the user/coder simply wants the parameter to begin without any spacing characters. But Plus!'s default behaviour is to take every occuring spacing character after the first spacing character after the command as part of the parameter (though not that usefull for almost all scripts).
^^ your reg exp fails here since you don't specify a spacing character ("\s") but only a space (ascii 32).
- a parameter can contain multiple lines.
^^ your reg exp fails here too since you check on 'any' character ("."), but 'any' character does not include new line characters ("\n").
Thanks for that, I always miss the minor details but at least you are there to fix me up <3
quote:
A proper generic regular expression to seperate the command and parameter:
code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage) {
if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
var command = RegExp.$1.toLowerCase();
var parameter = RegExp.$2;
switch (command) {
case 'quit':
// <- Do something when the user typed the command quit
return '';
case 'exit':
// <- Do something when the user typed the command exit
return '';
case 'cancel':
// <- Do something when the user typed the command cancel
return '';
}
}
}
Thanks, I'm noting this one down.
quote:
If you only have 1 command in your script (eg: /explode) your can use something like:code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage) {
if (/^\/explode\s*([\s\S]*)$/i.exec(sMessage) !== null) {
var parameter = RegExp.$1;
// <- Do something when the user typed the command explode
return '';
}
}
In above two snippets:
- to let the parameter include all leading spacing characters like Plus! nativly does, change \s* to \s?
I'm sorry to tell you but your expression also fails I'm sorry to say (but not as much as mine
). It should be this:
code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage) {
if (/^\/explode\s*([\s\S]*)$/.exec(sMessage) !== null) {//not case insensitive
var parameter = RegExp.$1;
// <- Do something when the user typed the command explode
return '';
}
}
Otherwise "/EXPLODE" would also work and any other instance of "/explode" that uses whichever case wherever through the word. I'll let you off this once though