Jump to content

I give up, pulling my hair out


Monkuar

Recommended Posts

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  :shy:

Link to comment
https://forums.phpfreaks.com/topic/267151-i-give-up-pulling-my-hair-out/
Share on other sites

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.

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.