Instantly share code, notes, and snippets.

What would you like to do?
Guid generator in Javascript.
var matcher = /[xy]/g;
var replacer = function(c) {
var r = Math.random()*16|0;
var v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
//maybe shouldn't be modifying the global variable, but seems like the best place to put this.
window.String.UUID = function(){
//see for more details.
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(matcher, replacer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment