Skip to content

Instantly share code, notes, and snippets.

@johnelliott
Last active April 10, 2024 07:31
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save johnelliott/cf77003f72f889abbc3f32785fa3df8d to your computer and use it in GitHub Desktop.
Save johnelliott/cf77003f72f889abbc3f32785fa3df8d to your computer and use it in GitHub Desktop.
uuid v4 regex
import { v4 as uuid } from 'uuid';
export function generateId() {
return uuid();
}
const v4 = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
console.log(generateId().match(v4));
//console.log(generateId().length)
//console.log('new way')
//console.log(generateId().length)
//console.log('new way, chopped')
//console.log(generateId().split('-')[0])
//console.log('old way')
//const generateNumber = () => Math.ceil(Math.random() * 100)
//console.log(`${generateNumber()}${generateNumber()}${generateNumber()}${generateNumber()}`)
// run with $ node_modules/.bin/babel-node testuuid.js
@gastrodon
Copy link

Why in the world would you alias v4 as uuid and THEN wrap it in generateId??

@groteck
Copy link

groteck commented Dec 14, 2021

@gastrodon the alias is common, since it is just the version and v4 is not a good naming. But I agree that generateId is not the best thing here since you can alias the v4 as generateId and will be the same outcome.
To me looks like this was part of a bigger file and was just extracted the meaningful parts for the test.

@jedsmith13
Copy link

I had to change the regex to lowercase as that seems to be what v4 is creating for me.

[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}

@juanmartinez-viamericas
Copy link

juanmartinez-viamericas commented Sep 28, 2022

supporting lower or upper case

^[0-9(a-f|A-F)]{8}-[0-9(a-f|A-F)]{4}-4[0-9(a-f|A-F)]{3}-[89ab][0-9(a-f|A-F)]{3}-[0-9(a-f|A-F)]{12}$

@nathandentzau
Copy link

The trailing /i means a case insensitive search. RE the last two comments.

@Mars073
Copy link

Mars073 commented Nov 21, 2022

@juanmartinez-viamericas A capture group (the parentheses) is not allowed in a range (the brackets), so your regex allows to put parentheses and pipres in uuids. And your "insensitive regex without flag" doesn't allow this range [89ab] in uppercase.

/^[0-9(a-f|A-F)]{8}-[0-9(a-f|A-F)]{4}-4[0-9(a-f|A-F)]{3}-[89ab][0-9(a-f|A-F)]{3}-[0-9(a-f|A-F)]{12}$/
  .test('376)5f8c-e686-4b(9-a85b-7e31|8f4062c') // returns true
//          ^            ^           ^
/^[0-9(a-f|A-F)]{8}-[0-9(a-f|A-F)]{4}-4[0-9(a-f|A-F)]{3}-[89ab][0-9(a-f|A-F)]{3}-[0-9(a-f|A-F)]{12}$/
  .test('376a5f8c-e686-4b79-A85b-7e31e8f4062c') // returns false
//                          ^

@gastrodon
Copy link

gastrodon commented Mar 21, 2023

Using regex to support upper/lowercase is totally bonkers. If you expect it to be uppercase / not, assert that the string is uppercase / not . If you don't know, just normalize your data first. Pointed out by @Mars073 the lower/upper regex also adds unnecessarily complexity that can break things

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