Skip to content

Instantly share code, notes, and snippets.

@bugventure
Last active November 30, 2021 10:16
Show Gist options
  • Save bugventure/f71337e3927c34132b9a to your computer and use it in GitHub Desktop.
Save bugventure/f71337e3927c34132b9a to your computer and use it in GitHub Desktop.
UUID regex matching in node.js
function createUUID() {
return uuid.v4();
}
// version 4
// createUUID.regex = '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$';
createUUID.regex = '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$';
createUUID.is = function (str) {
return new RegExp(createUUID.regex).test(str);
};
@jvalentik
Copy link

👍

@knurherb
Copy link

:D

@prodis
Copy link

prodis commented Jun 12, 2019

👍

@mattstabeler
Copy link

To use this in an express route

/:uuid([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})/

like

router.get(`/path/:uuid([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})/path`, 
    (req, res, next) => {
       const uuiid = req.params.uuid;
    }) {
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment