Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.