quote:
Originally posted by Jesus
quote:
Originally posted by CookieRevised
It also doesn't take in account other spacing characters than ascii 32 (spacebar), which can occur on the command line. To overcome all this use regular expressions.
If you aren't up to using regular expressions you can still use a combination of substr() and IndexOf().
Or easier, use the split() method but with the optional limit parameter:
stringObj.split(" ", 2). This will limit your array to 2 elements, and the second element will always be the parameter.
But also note that with any method you use (except for the regular expression method) you need to take in account additional leading and trailing spaces. Thus you also need to trim the parameter element before using it.
(and take in account the casing of letters too)
Cookie, I don't mean to criticise you, but the stringObj.split(" ", 2) method won't work any better than just using stringObj.split(" ").
The only difference is that with the optional limit parameter, your array is limited to 2 elements, without using the rest of the string.
eg:
code:
var str = "The quick brown fox jumps over the lazy dog.";
return str.split(" ",2);
will return a 2-element array: "The,quick"
so a command like "/command parameter" (2 spaces in between) will become "/command," with an empty 2nd element.
I had to try this to see it, as it's not in the docs (the limit parameter is, but the fact that it leaves the rest of the string isn't) so even with reading the docs you don't always get where you want
My conclusion is that regular expressions are the best way to go (unfortunately I don't know how to use them yet), but that using stringObj.toLowerCase().split(" ") is an acceptable second, which also takes casing of letters into account.
You could even use a loop to filter out the empty elements caused by extra spaces.
I totally agree with the rest of your post, as usual
I think what you are after in regular expressions is the follow, though if someone is will to suggest a better regular expression I will be happy to accept it (assuming it works).
code:
if((arr = /\/([^ \s]*) (.*)/i.exec(stringObj) != null){
var command = arr[1];
var param = arr[2];
}
Btw, im not sure if it worked as I'm busy trying to do other things (and too tired too test), but I'm quite sure it should work, the only thing I'm unsure of it the space character.
EDIT: Tested and fixed Regular Expression