Skip to content

Instantly share code, notes, and snippets.

@baldurh
Last active May 4, 2021 13:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save baldurh/aec96007a47fc15410b0 to your computer and use it in GitHub Desktop.
Save baldurh/aec96007a47fc15410b0 to your computer and use it in GitHub Desktop.
A regex for capturing different parts of cloudinary urls
const CLOUDINARY_REGEX = /^.+\.cloudinary\.com\/(?:[^\/]+\/)(?:(image|video)\/)?(?:(upload|fetch)\/)?(?:(?:[^_/]+_[^,/]+,?)*\/)?(?:v(\d+|\w{1,2})\/)?([^\.^\s]+)(?:\.(.+))?$/;
const url = 'http://res.cloudinary.com/oz/image/upload/v1454951830/moments/musicreach/f3432155-1be9-4510-9c3a-c1672c21fc89/f5de39a0-7ad0-48de-b53b-1d148856bcb8_540_pv.jpg';
const matches = CLOUDINARY_REGEX.exec(url);
console.log(matches);
/*
[
'http://res.cloudinary.com/oz/image/upload/v1454951830/moments/musicreach/f3432155-1be9-4510-9c3a-c1672c21fc89/f5de39a0-7ad0-48de-b53b-1d148856bcb8_540_pv.jpg',
'image', // resource_type
'upload', // type
'1454951830', // version
'moments/musicreach/f3432155-1be9-4510-9c3a-c1672c21fc89/f5de39a0-7ad0-48de-b53b-1d148856bcb8_540_pv', // public_id
'jpg', // format
index: 0,
input: 'http://res.cloudinary.com/oz/image/upload/v1454951830/moments/musicreach/f3432155-1be9-4510-9c3a-c1672c21fc89/f5de39a0-7ad0-48de-b53b-1d148856bcb8_540_pv.jpg'
]
/*
// Unformatted regex:
// ^.+\.cloudinary\.com\/(?:[^\/]+\/)(?:(image|video)\/)?(?:(upload|fetch)\/)?(?:(?:[^_/]+_[^,/]+,?)*\/)?(?:v(\d+|\w{1,2})\/)?([^\.^\s]+)(?:\.(.+))?$
// Regex capturing information
// Capturing groups
// -------------------------------^ resource_type (1)
// ---------------------------------------------------^ type (2)
// ----------------------------------------------------------------------------------------------------^ version (3)
// ---------------------------------------------------------------------------------------------------------------------^ public_id (4)
// -------------------------------------------------------------------------------------------------------------------------------------^ format (5)
// Non-capturing groups
// ----------------^ cloud name (with backslash)
// ----------------------------^ optional resource_type (with backslash)
// ------------------------------------------------^ optional type (with backslash)
// ---------------------------------------------------------------------^ optional transformations (with backslash)
// ------------------------------------------------------------------------^ transformation group (repeated)
// ------------------------------------------------------------------------------------------------^ optional version (including 'v', with backslash)
// --------------------------------------------------------------------------------------------------------------------------------^ optional extension (including '.')
// See it working here: http://www.regexr.com/3d83n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment