Skip to content Skip to sidebar Skip to footer

Fix/escape Javascript Escape Character?

see this answer for reasoning why / is escaped and what happens on nonspecial characters I have a string that looks like this after parsing. This string comes fron a javascript lin

Solution 1:

Use the String.Replace() method:

string expr = @"http:\/\/www.site.com\/user";  // That's what you have.
expr = expr.Replace("\\/", "/");               // That's what you want.

That, or:

expr = expr.Replace(@"\/", "/");

Note that the above doesn't replace occurrences of \ with the empty string, just in case you have to support strings that contain other, legitimate backslashes. If you don't, you can write:

expr = expr.Replace("\\", "");

Or, if you prefer constants to literals:

expr = expr.Replace("\\", String.Empty);

Solution 2:

I am not sure why it has the \ in it since var foo = "http://www.site.com/user"; is a valid string.

If the \ characters are meant to be there for some strange reason, than you need to double them up

var foo ="http:\\/\\/www.site.com\\/user";

Post a Comment for "Fix/escape Javascript Escape Character?"