Shoutbox

Just adding a couple of lines, and my script doesn't work anymore! - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Just adding a couple of lines, and my script doesn't work anymore! (/showthread.php?tid=94263)

Just adding a couple of lines, and my script doesn't work anymore! by tj0nge on 03-31-2010 at 09:39 PM

Hello, all of you.
I'm making a script, just for fun, and I don't think I'm ever going to publish it, just for learning, but I have a problem now. The script translates 'normal' language into binary code, and it works pretty good. at least, until I start to add symbols (!,/,*,&,%,$, etc...) to translate. Than I get an error message, wich says my script is broken or I do not have enough rights...

can anybody help?

PS:
[/code]function translate(Message)
{
       
        (...)
    Message = Message.replace(/<symbol/letter>/gi, "<binary code, like:    00100000> ");
    (...)
   
    return Message;
}[code]


RE: Just adding a couple of lines, and my script doesn't work anymore! by foaly on 03-31-2010 at 09:41 PM

did you escape the symbols that are used in regexps?


RE: Just adding a couple of lines, and my script doesn't work anymore! by CookieRevised on 04-01-2010 at 03:12 AM

Some symbols have special meanings in regular expressions (and strings). And thus you can't simply put them in, you need to escape them first. Hence:

quote:
Originally posted by foaly
did you escape the symbols that are used in regexps?


Escaping such symbols means you put the 'escape' symbol in front of them. For regular expressions (and many other subcode languages) the escape symbol is the slash: \

eg: Message = Message.replace(/\!/gi, "01010101");

Note: you must do the same for paths in strings. Or any occurence of the character \ in a string (if \ is meant to be taken as a literal character).
eg: var myPath = "C:\Documents"
will return an error, because \ is a special character (the escape symbol), so you must escape the escape symbol to make it a literal character:
eg: var myPath = "C:\\Documents"
the output of the myPath variable will be C:\Documents

--------------------

But as with many things: you can avoid the whole thing and make your script hundreds of lines and probably thousands of bytes shorter (and waaaay faster), by tackling your particular problem in the proper way:
Instead of doing a replace using regular expressions for each character, it would be way faster and shorter to simply take the decimal Ascii code (which is base 10) of the character and simply convert it into a binary code (base 2).