Monkuar Posted August 16, 2012 Share Posted August 16, 2012 message is my documentbyid for the input box var oldmessage = message.substring(3); var username = oldmessage.split(/\b/)[0]; var realmessage = message.split(" ", 3)[2]; //Check if the user inputs /w to send a message var commands = message.substring(0,2); switch (commands){ case "/w": alert(username); alert(realmessage); break; } I am making a script where if a user does /w USERNAME message here blah blah.... right now, I am testing it with: "/w Rob hello mayne!" It works as expected, except..... any text that is from the RIGHT of "hello" are not shown. Why? Because I am using this: var realmessage = message.split(" ", 3)[2]; And that only grabs the "hello" I need to grab everything to the right of that too, as that will be the message they will be sending! If anyone has any insight... Please help Quote Link to comment https://forums.phpfreaks.com/topic/267151-i-give-up-pulling-my-hair-out/ Share on other sites More sharing options...
Christian F. Posted August 16, 2012 Share Posted August 16, 2012 How about just refining the RegExp you're already using, and dropping the split () completely? var whisperReg = /^\/w\s+(\w+)\s+(.*)/ That'll fetch the username as group 1, and the message in group 2. Quote Link to comment https://forums.phpfreaks.com/topic/267151-i-give-up-pulling-my-hair-out/#findComment-1369756 Share on other sites More sharing options...
Monkuar Posted August 16, 2012 Author Share Posted August 16, 2012 How about just refining the RegExp you're already using, and dropping the split () completely? var whisperReg = /^\/w\s+(\w+)\s+(.*)/ That'll fetch the username as group 1, and the message in group 2. var username = message.split(/^\/w\s+(\w+)\s+(.*)/)[1]; var newmessage = message.split(/^\/w\s+(\w+)\s+(.*)/)[2]; //Check if the user inputs /w to send a message var commands = message.substring(0,2); switch (commands){ case "/w": alert(username); alert(newmessage); break; } Works great now, topic solved... I swear I need to learn more advanced REGEX, would save my ass so many times. Appreciate your help for this Christian. Quote Link to comment https://forums.phpfreaks.com/topic/267151-i-give-up-pulling-my-hair-out/#findComment-1369758 Share on other sites More sharing options...
Christian F. Posted August 16, 2012 Share Posted August 16, 2012 A bit better version: temp = message.split (whisperReg); if (is_string (temp[1])) { // User used "/w". var username = temp[1]; var message = temp[2]; } No need calling split () twice, no a need to test against the command. At least not for just one. Though, if you want more just tweak the RegExp a bit, and you can retrieve the command as group 1 (adding 1 to the indices of the other groups). Quote Link to comment https://forums.phpfreaks.com/topic/267151-i-give-up-pulling-my-hair-out/#findComment-1369765 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.