0

I'm pulling JSON into objc and need to escape double quotes in only part of the string. I assume that I'll have to use regex but can't wrap my mind around it. Simply using stringByReplacingOccurrencesOfString: replaces every occurrence of the double quotes.

Input (example):

[ { "name": "Bob", "formatted_name": "<p id="formattedName">Bob</p>" }, { "name": "David", "formatted_name": "<p id="formattedName">David</p>" } ]

Output:

[ { "name": "Bob", "formatted_name": "<p id=\"formattedName\">Bob</p>" }, { "name": "David", "formatted_name": "<p id=\"formattedName\">David</p>" } ]
SirJay
  • 119
  • 4
  • 8
  • 1
    Why do you need to do this? The escaping should have been done by the library on sending and receiving side. – nhahtdh Nov 03 '14 at 04:09
  • use this regex `(?<=\S)"(?!\S?\s)` http://regex101.com/r/lL9kG4/3 – Avinash Raj Nov 03 '14 at 04:14
  • [JSON is not a regular language](http://en.wikipedia.org/wiki/Pumping_lemma_for_context-free_languages) [(more details here)](http://cstheory.stackexchange.com/questions/3987/is-json-a-regular-language), so you should be [very careful about parsing JSON or other non-regular languages with regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). However, for the odd specific use, regex is totally fine. – Morgan Chen Nov 03 '14 at 04:16

0 Answers0