Giter Site home page Giter Site logo

base64-regex's Introduction

base64-regex Build Status

Regular expression for matching base64 encoded strings

Install

$ npm install --save base64-regex

Usage

const base64Regex = require('base64-regex');

base64Regex().test('dW5pY29ybg== foo bar');
//=> true

base64Regex({exact: true}).test('dW5pY29ybg== foo bar');
//=> false

base64Regex({exact: true}).test('dW5pY29ybg==');
//=> true

'foo dW5pY29ybg== bar Ym9hdA=='.match(base64Regex());
//=> ['dW5pY29ybg==', 'Ym9hdA==']

API

base64Regex([options])

Returns a regex for matching base64 encoded strings.

options

exact

Type: boolean
Default: false (Matches any base64 in a string)

Only match an exact string. Useful with RegExp#test to check if a string is a base64 encoded string.

License

MIT © Kevin Mårtensson

base64-regex's People

Contributors

kevva avatar munter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

base64-regex's Issues

Published version on npm is not same as in repo

index.js in the repository has an extra ? in the exact string interpolation, causing them to validate different things.

Published version: https://cdn.jsdelivr.net/npm/[email protected]/index.js
In repository: https://github.com/kevva/base64-regex/blob/0f0065cb1a1c75dc13c603c424f19103a2294782/index.js

I believe neither of the versions to be correct though, since the ? should be present in the regex for both exact and non-exact in order to match all base64 strings. Without it, it only matches 2/3 of all possible base64 strings.

File does not validate as base64

This PDF-file does not validate as a valid base64. Any idea why?

const fs = require('fs')
const base64Regex = require('base64-regex')
const b64 = fs.readFileSync('./smalltest.pdf' ,'base64')
console.log(b64) // missing == at end
console.log(base64Regex().test(b64)) // false

Regex doesn't validate base64 not ending in =

If you look at the regex, you'll see that it requires a segment that ends in a =. For example, QmCC is a valid base64 string, but this regex doesn't validate that string:

https://regex101.com/r/uGf7ne/1

However, ^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$ allows the last segment to not have to end in an =.

https://regex101.com/r/uGf7ne/2

I might also add that I've seen other people lift this regex for other uses (https://stackoverflow.com/a/29275028), which I'm also doing, thus I found this problem. In that case, it might be helpful to the greater community to make the regex more 'flavor compatible' by escaping some characters that other languages require, like so: ^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$

I'd be happy to create a PR if so desired.

Large base64 buffer not validate

When i try to validate large audio base64 buffer i got below error -

RangeError: Maximum call stack size exceeded

Audio file size approx 5MBb

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.