What I am trying to do is write a script in Matlab that will parse some HTML, get some data out of it, take this data, and then format it into a Javascript string array. I do not have a problem grabbing HTML and parsing it and so forth, I am having trouble trying to print a string using sprintf that will contain a backslash.
Basically if you have a string in Javascript that contains a quotation mark you need to escape it with a backslash:
var string1 = "Here is a \"string\" example";
When I try to do this in Matlab as follows, it does not print correctly:
>> A = sprintf('Here is a \"string\" example')
A =
Here is a "string" example
This is not a valid Javascript string. So basically I want sprintf to return a proper Javascript string; any suggestions?
I have tried using \\" and \\\" and a few similar combinations, all to no avail.
Matlab uses the reference Kernighan, B. W., and D. M. Ritchie, The C Programming Language for its sprintf function. According to the reference, the way sprintf is defined, it uses escape character as a way to overcome the default control meaning of characters.
Although you asked not to suggest the combination like
here is the viable solution where you don’t have to manually replace the \” with \\” and the code does this action itself.