javascript代码
function guid () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0
var v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
去除横线
function guidShort () {
return guid ().replace("-","");
}
显示结果
UUID完整:4915d2f4-2e83-47d6-bc76-e321caeff812
UUID不含-:f5e53a50fda8-4c8d-a4fb-04821ac932d0
完整Demo
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
function guid () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0
var v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
function guidShort () {
return guid ().replace("-","");
}
$(function(){
$("#div1").text('UUID完整:'+guid ());
$("#div2").text('UUID不含-:'+guidShort ());
});
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>