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

Pages: (3): « First [ 1 ] 2 3 » Last »
Replace Colour Codes
Author: Message:
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
O.P. Replace Colour Codes
I'm trying to convert a string containing BBcode colours into the old 3.xx style. It seems to work partially (It converts the first few, but not others). This is the code I'm using at the moment:

code:
    for(var a=0;a<=54;a++){
    var before = "[c="+a+"]";
    var after = "·$"+a+" ";
    strMyNewPSM = strMyNewPSM.replace(before,after);
    }
    Debug.Trace(strMyNewPSM);   
    strMyNewPSM = strMyNewPSM.replace('[/c]','·$1');
    Messenger.MyPersonalMessage = strMyNewPSM;


Any ideas why its not working properly?

Also, I need to remove leading spaces... I know how to check for them, but not how to remove them.
<Eljay> "Problems encountered: shit blew up" :zippy:
09-07-2006 08:20 PM
Profile PM Find Quote Report
alexp2_ad
Scripting Contest Winner
****

Avatar
Who love the chocolate?

Posts: 691
Reputation: 26
36 / Male / –
Joined: May 2004
Status: Away
RE: Replace Colour Codes
JScript's replace function only replaces the first occurence in a string.  You can use regex (regular expressions) to get them all, but I'm only good with simple regex, so you can wait for a regex master to help. :P
09-07-2006 08:31 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
O.P. RE: Replace Colour Codes
I'm just doing some research on that now... I've seen it used in other scripts and it baffles me :p

I'll let you know if I manage it ;)
<Eljay> "Problems encountered: shit blew up" :zippy:
09-07-2006 08:32 PM
Profile PM Find Quote Report
KnRd_WC
Junior Member
**

Avatar
Florian PAQUET

Posts: 74
Reputation: 1
34 / Male / Flag
Joined: Aug 2006
RE: Replace Colour Codes
Hi ! ;)

I don't know if it's the best way to do what you want, but maybe you can try this :

code:
var MyPSM = "[c=1]Color1[/c][c=2]Color2[/c]";
for(var a=0;a<=54;a++){
     RgEx = new RegExp("[[]+c="+a+"+[]]","gi");
     MyPSM = MyPSM.replace(RgEx,"·$"+a);
}
RgEx = new RegExp("[[]+/c+[]]","gi");
MyPSM = MyPSM.replace(RgEx,"·$");
MsgPlus.DisplayToast("",MyPSM);


I've got a lot of Web sites which explain how RegExp works... but they're only in French... :s

This post was edited on 09-07-2006 at 09:18 PM by KnRd_WC.
09-07-2006 09:15 PM
Profile PM Web Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Replace Colour Codes
Replacing can also be done with a simple loop.

Here's a function I made that replaces the default String.replace function. I haven't tested it thoroughly, but I'm sure the bugs are nothing complicated ;)

To use it, use the normal String.replace function. However, there is an extra parameter. "Count" is an integer, it'll specify how many occurrences to replace in the string. If you leave it out, it will replace all of them.

code:
String.prototype.replace = function(term,replacement,count){
    var string = this;
    if(count == null){
        while(string.indexOf(term) > -1){
            string = string.slice(0,string.indexOf(term)) + replacement + string.slice(string.indexOf(term)+term.length,string.length);
        }
    } else if(typeof count == 'number'){
        for(var i = 0; i < count; i++){
            string = string.slice(0,string.indexOf(term)) + replacement + string.slice(string.indexOf(term)+term.length,string.length);
        }
    }
    return string;
}
09-07-2006 09:17 PM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
O.P. RE: Replace Colour Codes
Thanks, been talking with SilentDragon on WLM and we seem to have sorted it out :D

I'll post the code for others that are looking for the same thing so it does not get asked again :p

code:
for(a=0;a<=54;a++){

    strMyNewPSM = strMyNewPSM.replace(new RegExp("\\[c="+a+"\\]",'gi'),"·$"+a+" ");
    strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/g,'·$1');
   
    }
    strMyNewPSM = strMyNewPSM.replace(/ [\s]/g,"");


EDIT: My code was using a loop to change EACH colour individually so putting it in another loop was the only alternative... After trying this I had to close WLM and edit the file in Notepad as it caused a wierd loop error :p

This post was edited on 09-07-2006 at 09:22 PM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
09-07-2006 09:18 PM
Profile PM Find Quote Report
Shondoit
Full Member
***

Avatar
Hmm, Just Me...

Posts: 227
Reputation: 15
35 / Male / Flag
Joined: Jul 2006
RE: Replace Colour Codes
I believe this should work, it worked for me...
code:
strMyNewPSM = strMyNewPSM.replace(/\[c=([1-4]?\d|5[0-4])\]/gi, "·\$$$1")
strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/gi, "·\$");
strMyNewPSM = strMyNewPSM.replace(/^\s+/g, "");

And no lopes at all (y)

I had to search how to get the '$'-sign as the replace text, because $1-$9 are used for backreference... (putting part of the matched text in the replaced text)

If want/need to know how it works, just ask me on WLM sometimes...

This post was edited on 09-08-2006 at 03:43 PM by Shondoit.
My scripts:                            [Image: shondoit.gif]
+ Timezone
+ Camelo
+ Multisearch
09-08-2006 03:42 PM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: Replace Colour Codes
In fact, there never was need to use a loop for it. By placing the moderator "g" (global) after the end slash of a regular expression, it'll look for all matches and replace all matches. ;)

Also, don't forget people can use hexadecimal codes like [c=#ff00ff]...[/c]!!! :o
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
09-08-2006 04:17 PM
Profile E-Mail PM Web Find Quote Report
Shondoit
Full Member
***

Avatar
Hmm, Just Me...

Posts: 227
Reputation: 15
35 / Male / Flag
Joined: Jul 2006
RE: Replace Colour Codes
Yeah, correct, for support for the hex codes:...
code:
strMyNewPSM = strMyNewPSM.replace(/\[c=([1-4]?\d|5[0-4]|#[0-9A-F]{6})\]/gi, "·\$$$1")
strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/gi, "·\$");
strMyNewPSM = strMyNewPSM.replace(/^\s+/g, "");


-edit- I just found out, that the 'c' in the tag is case sensitive, only lowercase is allowed and also the numbers go 'til 69 :S, therefore this would be a better solution
code:
strMyNewPSM = strMyNewPSM.replace(/\[c=([1-6]?\d|#[0-9A-Fa-f]{6})\]/g, "·\$$$1")
strMyNewPSM = strMyNewPSM.replace(/\[\/c\]/g, "·\$");
strMyNewPSM = strMyNewPSM.replace(/^\s+/g, "");


This post was edited on 09-08-2006 at 04:33 PM by Shondoit.
My scripts:                            [Image: shondoit.gif]
+ Timezone
+ Camelo
+ Multisearch
09-08-2006 04:21 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Replace Colour Codes
quote:
Originally posted by Mattike
Also, don't forget people can use hexadecimal codes like [c=#ff00ff]...[/c]!!! :o
and gradients, and background colors, ... and tags are only valid if there is a closing tag, ...and numbers can include a leading 0, and hexadecimal numbers are only read for the first 6 characters/ the rest is ignored but can exist! ...

It is not so strait forward to make something like this (but no impossible; you just need to think about all the possebilities there are).

This post was edited on 09-08-2006 at 04:40 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-08-2006 04:35 PM
Profile PM Find Quote Report
Pages: (3): « First [ 1 ] 2 3 » Last »
« 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