Search code examples
node.jsstringvalidationbase64

How to check if a string is plaintext or base64 format in Node.js


I want to check if the given string to my function is plain text or base64 format. I am able to encode a plain text to base64 format and reverse it. But I could not figure out any way to validate if the string is already base64 format. Can anyone please suggest correct way of doing this in node js? Is there any API for doing this already available in node js.


Solution

  • Encoding is byte level. If you're dealing in strings then all you can do is to guess or keep meta data information with your string to identify

    But you can check these libraries out:

    1. https://www.npmjs.com/package/detect-encoding
    2. https://github.com/mooz/node-icu-charset-detector