I'm struggling to parse a key:value pair in a JSON-like string. I know people will automatically say "Use JSON.parse()
for this!" and I absolutely agree. The problem is that I'm not dealing with JSON strings, but JSON-like strings.
At least my attempts of parsing these strings with JSON.parse have failed (I've tried to sanitize the string so that JSON.parse doesn't complain about malformed strings)
The problem I have is that the JSON-like string sometimes is truncated and some other times is not. What is guaranteed to happen is that the key publicProfileUrl
will be in the text, all the time (or at least that's been consistent with observations) and I need to parse its value:
For example, this is an example of the string:
%%"fullName":"Eduardo Saverin",
"contactInfo":{
"publicProfileUrl":"https://sg.linkedin.com/in/saverin",
"twitterAccounts":["esaverin"],
"websites":[]},
"industry":"Internet",%%
all I'm interested in is parsing the value of publicProfileUrl.
This is my latest attempt at doing it:
\"publicProfileUrl\":\"(.*)\",
but it is matching all the way to the last comma (I added line breaks for formatting purposes only, but the original string doesn't have any line breaks).
Here's the original string:
%%"fullName":"Eduardo Saverin","contactInfo":{"publicProfileUrl":"https://sg.linkedin.com/in/saverin","twitterAccounts":["esaverin"],"websites":[]},"industry":"Internet",%%