Replace Colour Codes |
Author: |
Message: |
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
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"
|
|
09-07-2006 08:20 PM |
|
|
alexp2_ad
Scripting Contest Winner
Who love the chocolate?
Posts: 691 Reputation: 26
37 / / –
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.
|
|
09-07-2006 08:31 PM |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
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
I'll let you know if I manage it
<Eljay> "Problems encountered: shit blew up"
|
|
09-07-2006 08:32 PM |
|
|
KnRd_WC
Junior Member
Florian PAQUET
Posts: 74 Reputation: 1
35 / /
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...
This post was edited on 09-07-2006 at 09:18 PM by KnRd_WC.
|
|
09-07-2006 09:15 PM |
|
|
deAd
Scripting Contest Winner
Posts: 1060 Reputation: 28
– / /
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 |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
O.P. RE: Replace Colour Codes
Thanks, been talking with SilentDragon on WLM and we seem to have sorted it out
I'll post the code for others that are looking for the same thing so it does not get asked again
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
This post was edited on 09-07-2006 at 09:22 PM by Spunky.
<Eljay> "Problems encountered: shit blew up"
|
|
09-07-2006 09:18 PM |
|
|
Shondoit
Full Member
Hmm, Just Me...
Posts: 227 Reputation: 15
36 / /
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
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.
|
|
09-08-2006 03:42 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
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]!!!
|
|
09-08-2006 04:17 PM |
|
|
Shondoit
Full Member
Hmm, Just Me...
Posts: 227 Reputation: 15
36 / /
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 , 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.
|
|
09-08-2006 04:21 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
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]!!!
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 |
|
|
Pages: (3):
« First
[ 1 ]
2
3
»
Last »
|
|