Reputation: 11081
Is there a simple way to check if string is valid UTF-8 sequence in JavaScript?
I really do not want to end with a regular expression like this:
Regex to detect invalid UTF-8 string
P.S.: I am receiving data from external API and sometimes (very rarely but it happens) it returns data with invalid UTF-8 sequences. Trying to put them into PostgreSQL results in an appropriate error.
Upvotes: 5
Views: 5885
Reputation: 20885
UTF-8 is in fact a simple encoding, but still what you are asking can't be done with a one-liner. You have to:
Content-Type
of the response to have a byte array in your script and prevent the browser/library to interpret the response itselfDeciding if a certain array is a valid UTF-8 sequence is quite a straightforward task (just a bunch of if
statements and bit shiftings), but again it's not a one line thing.
Upvotes: 5