- URL解码(UrlDecode):
function urlDecode(input) {
return decodeURIComponent(input.replace(/\+/g, ' '));
}
// 使用示例
var encodedUrl = "https%3A%2F%2Fwww.example.com%2Fsearch%3Fquery%3Djavascript";
var decodedUrl = urlDecode(encodedUrl);
console.log(decodedUrl); // 输出: https://www.example.com/search?query=javascript
- UUID和GUID生成:
function generateUUID() {
var d = new Date().getTime(); //Timestamp
var d2 = (performance && performance.now && (performance.now()*1000)) || 0; //Time in microseconds since page-load or 0 if unsupported
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16; //random number between 0 and 16
if(d > 0) { //Use timestamp until depleted
r = (d + r)%16 | 0;
d = Math.floor(d/16);
} else { //Use microseconds since page-load if supported
r = (d2 + r)%16 | 0;
d2 = Math.floor(d2/16);
}
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
// 使用示例
var uuid = generateUUID();
console.log(uuid); // 输出: 一个UUID,例如: 57dd3176-1774-4009-935d-93e1334b
- 阿拉伯数字转中文数字:
function numberToChinese(num) {
var chnNumChar = ["零","一","二","三","四","五","六","七","八","九"];
var chnUnitChar = ["","十","百","千","万","十","百","千","亿"];
var strIn = num.toString();
var chnStr = "";
var unitPos = 0;
var zero = true;
for (var i = 0; i < strIn.length; i++) {
var num = strIn.charAt(i);
if (num != 0) {
zero = false;
}
if (!zero || (num != 0 && i % 4 == 0 && i != strIn.length -1 && strIn.charAt(i+1) != 0)) {
chnStr += chnNumChar[Number(num)] + chnUnitChar[unitPos];
} else if (num == 0 && unitPos == 0 && chnStr.length == 0) {
chnStr += chnNumChar[0];
}
unitPos++;
if (unitPos == 9 && num == 0) {
unitPos = 0;
}
if (unitPos == 9) {
if (zero) {
chnStr += chn
评论已关闭