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 fro开发者_JS百科n a javascript line.
var="http:\/\/www.site.com\/user"
I grabbed the inside of the quote so all i have is http:\/\/www.site.com\/user
. How do i properly escape the string? so its http://www.site.com/user
? I am using .NET
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);
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";
精彩评论