While trying to handle a string I am facing
Error: SyntaxError: Unexpected EOF
I have no control on how the string is generated. I managed to isolate the problematic character(s) by dichotomy with substring()
, however it doesn't get printed either by console.log()
or by JSON.parse()
: I get
> console.log(c);
""
> JSON.parse(c);
""
yet
> c.length;
1
All I know is that it is followed by a \n
.
How can I identify it and get rid of it ?
> console.log(encodeURIComponent(c));
%E2%80%A8
Assuming this is the only problematic character, and since its percent-encoding has been identified, a solution is to replace the decoded percent-encoding string :
> c.length;
1
> let badchar = decodeURI("%E2%80%A8");
> let regex = new RegExp(badchar, "g");
> newc = c.replace(regex, "");
> newc.length;
0
Or if for some reason RegExp
is not available :
> newc = c.split(badchar).join("")