Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use \ in a JavaScript string without escaping?

Tags:

javascript

I have a string that is automatically generated by some code that encodes a Google Static Map polyline set of longitude,latitude. However, it places these pesky backslashes in the string which tries to escape the character after it.

enc:{eggEhwnQDYOCZuDv@q@H}@v@k@^v@TQh@Aw@j@AJJ@CZKA?LNZ[RUEALDDCRC@AJBBCJ\FAEACC?GM?K@GDGDEJC@BDADBFB@F@HANGH?DB@D\W|@g@QIZm@I@YoAO

I am not putting the encode directly into the HTML (where it would be fine) but instead using JavaScript to do it so this polyline encode gets put into a variable like so:

mapcoords:"path=color:0x00000000|fillcolor:0xFF9999|enc:{eggEhw`nQDYOCZuDv@q@H}@v@k@^v@TQh@`Aw@j@AJJ@CZKA?LNZ[RUEALDDCRC@AJBBCJ\FAEACC?GM?K@GDGDEJC@BDADBFB@F@HANGH?DB@D\W|@g@QIZm@I@YoAO"

Any suggestions how I go around the escaping? I've looked for the ampersand symbol for backslash but it seems one does not exist (if it would even help). So I am not sure how else to go about this.

like image 951
Christine268 Avatar asked Nov 23 '25 05:11

Christine268


2 Answers

You have to escape the backslash with a backslash like this:

var some_string = "my string with a backslash here: \\ ";

Most editors today have a find/replace function that you can use to replace a single backslash with two backslashes. If you use Notepad++ you can use CTRL+H to access this function, but as I said, most recent editors and IDE's have this function.

like image 59
Mateo Hrastnik Avatar answered Nov 24 '25 19:11

Mateo Hrastnik


All you need to do is escape the escape character, so you simply get \\ instead of a single \. You will have to do this replacement wherever it is you're outputting that string to the client.

like image 21
David Mulder Avatar answered Nov 24 '25 17:11

David Mulder