Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Forked from baldurh/cloudinary-url-regex.js
Created August 27, 2019 09:45
Show Gist options
  • Save ahmadawais/9813c44b7e51c2c3540d2165d6c6cc65 to your computer and use it in GitHub Desktop.
Save ahmadawais/9813c44b7e51c2c3540d2165d6c6cc65 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