This is a string that displays as formatted x^2:
"\!\(x \^2 \)"
If I left off the closing parenthesis for example:
"\!\(x \^2 "
Upon evaluation the front end shows a red bracket and a useless message which is simply "The String"
Question is how can we programmatically test for a valid string? Check doesn't seem to catch it. I'm supposing actually parsing the full string syntax would get pretty involved.
This came up while thinking about this question How to recover "pretty" format after split? by the way.
Edit: example illustrating a valid string where SyntaxQ will report False
"\!\( x \^2 = \)"


SyntaxQ. – Leonid Shifrin Dec 28 '15 at 20:57SyntaxQindeed catches the error. Its not a fully general solution however because it will false negative on a properly formatted expression that is not itself a syntactically correct expression. – george2079 Dec 28 '15 at 21:06x^2 =, which isn't the correct syntax. So at least you need to change it to something else, to illustrate your point. It isn't clear to me, for example, that there exists a single example of "anything that the front end displays without red flagging the cell", on whichSyntaxQgivesFalse. – Leonid Shifrin Dec 28 '15 at 22:04