What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Replace() Help

Replace() Help
Author: Message:
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
O.P. Replace() Help
I searched the forums for this, but the code doesnt seem to quite work in the exact way I want *-)

I'm making a script that converts a message to a code (in group convos, only people that have the script will recieve the decoded message). I can replace letters, but only as long as it is an exact match (ie it can't have any characters either side of the one I want to replace). 

I'm going to use a multi-dimensional array to store the code like so:
code:
myCode = [["a"]["XyZ"],["A"]["xYz"]];

Thats obviously just an example, but I want to replace ALL "a"s in the message with "XyZ". So I was thinking something like:
code:
tmpMessage = tmpMessage.replace(myCode[0][0],myCode[0][1]);

inside a For Loop.

I know the code seems pointless, but me and my friend usually have group conversations and still talk in a seperate window as well so the other participants don't have a clue what we're talking about.
<Eljay> "Problems encountered: shit blew up" :zippy:
08-19-2006 12:17 AM
Profile PM Find Quote Report
Silentdragon
Full Member
***

Avatar
if(life==null && wrists) EmoAlert();

Posts: 148
Reputation: 2
34 / Male / –
Joined: Jun 2006
RE: Replace() Help
code:
var myCode = [ [new RegExp("a","g"),"XyZ"],[new RegExp("A","g"),"xYz"] ];
    tmpMessage = tmpMessage.replace(myCode[0][0],myCode[0][1]);
    tmpMessage = tmpMessage.replace(myCode[1][0],myCode[1][1]);

1. You made your arrays wrong [ ["Stuff","Stuff2"], ["Stuff3","Stuff4"] ] Thats how to make a multidimensional
2. For replace you need either /texttofind/options or a RegExp object for the first argument.
08-19-2006 12:36 AM
Profile E-Mail PM Web Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
O.P. RE: Replace() Help
Ok, I knew I did it different before lol :P

Whats the "new RegExp" part do then?

This post was edited on 08-19-2006 at 12:39 AM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
08-19-2006 12:38 AM
Profile PM Find Quote Report
Silentdragon
Full Member
***

Avatar
if(life==null && wrists) EmoAlert();

Posts: 148
Reputation: 2
34 / Male / –
Joined: Jun 2006
RE: Replace() Help
it creates a RegExp object for the replace function
although a better way would be to do this in the for loop
var RegEx = new RegExp(myCode[i][0],"g");
tmpMessage = tmpMessage.replace(RegEx,myCode[i][1]);

so you don't need to make a new RegExp object for each array value.
08-19-2006 12:44 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On