Christine268
Christine268

Reputation: 732

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

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.

Upvotes: 3

Views: 3580

Answers (2)

Mateo Hrastnik
Mateo Hrastnik

Reputation: 543

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.

Upvotes: 6

David Mulder
David Mulder

Reputation: 27010

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.

Upvotes: 1

Related Questions