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