1

Using C# .net I am parsing some data with some partial html/javascript inside it (i dont know who made that decision) and i need to pull a link. The link looks like this

http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg

It came from this which i assume is javascript and looks like json

"name":{"id":"589","src":"http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg"}

But anyways how should i escape the first link so i get http://fc0.site.net/fs50/i/2009/name.jpg

In this case i could just replace '\' with '' since links dont contain \ nor " so i could do that but i am a fan of knowing the right solution and doing things properly. So how might i escape this. After looking at that link for a minute i thought is that valid? does java script or json escape / with \? It doesnt seem like it should?

4 Answers 4

1

In your case:

"name":{"id":"589","src":"http://fc0.site.net/fs50/i/2009/name.jpg"}

"/" is a valid escape sequence. However, it is not required that / be escaped. You may escape it if you need to. The reason JSON explicitly allows escaping of slash is because HTML does not allow a string in a to contain "...

Update:

Sign up to request clarification or add additional context in comments.

2 Comments

I think you misread a part. I already do "" in my C# strings and dont see why you mentioned that. I asked how do i escape the 1st string in C#.
@acidzombie24: sorry about that. I'll read well next time. Hope my answer helps you.
0

Odd, it doesn’t look like any JavaScript/JSON escaping you’d expect. You can have forward slashes in JavaScript strings just fine.

Comments

0

Why dont you try a regex on the escaped slashes to replace them in the C# code...

String url = @"http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg";

String pattern = @"\\/";

String cleanUrl = Regex.Replace(url, pattern, "/");

Hope it helps!

1 Comment

Not sure Jamie Zawinski would agree
0

Actually you want to unescape the string. Answered in this question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.