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 Answers4

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:

Community
  • 1
  • 1
Orson
  • 14,981
  • 11
  • 56
  • 70
  • 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#. –  Dec 20 '09 at 14:29
  • 1
    @acidzombie24: sorry about that. I'll read well next time. Hope my answer helps you. – Orson Dec 20 '09 at 14:38
0

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

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
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!

0

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

Community
  • 1
  • 1
Amnon
  • 7,652
  • 2
  • 26
  • 34