JS 去掉指定字符(附GUID生成)

js中如果想要去掉字符串中某个特定的字符,且这个字符数量不止一个的时候

用replace是不行的,这么做只能去掉第一个,需要分两步

str.split('-').join('')

 GUID生成

 1 function GUID() {
 2     this.date = new Date();   /* 判断是否初始化过,如果初始化过以下代码,则以下代码将不再执行,实际中只执行一次 */
 3     if (typeof this.newGUID != 'function') {   /* 生成GUID码 */
 4         GUID.prototype.newGUID = function () {
 5             this.date = new Date(); var guidStr = '';
 6             sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
 7             sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
 8             for (var i = 0; i < 9; i++) {
 9                 guidStr += Math.floor(Math.random() * 16).toString(16);
10             }
11             guidStr += sexadecimalDate;
12             guidStr += sexadecimalTime;
13             while (guidStr.length < 32) {
14                 guidStr += Math.floor(Math.random() * 16).toString(16);
15             }
16             return this.formatGUID(guidStr);
17         }
18         /* * 功能:获取当前日期的GUID格式,即8位数的日期:19700101 * 返回值:返回GUID日期格式的字条串 */
19         GUID.prototype.getGUIDDate = function () {
20             return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
21         }
22         /* * 功能:获取当前时间的GUID格式,即8位数的时间,包括毫秒,毫秒为2位数:12300933 * 返回值:返回GUID日期格式的字条串 */
23         GUID.prototype.getGUIDTime = function () {
24             return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero(parseInt(this.date.getMilliseconds() / 10));
25         }
26         /* * 功能: 为一位数的正整数前面添加0,如果是可以转成非NaN数字的字符串也可以实现 * 参数: 参数表示准备再前面添加0的数字或可以转换成数字的字符串 * 返回值: 如果符合条件,返回添加0后的字条串类型,否则返回自身的字符串 */
27         GUID.prototype.addZero = function (num) {
28             if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
29                 return '0' + Math.floor(num);
30             } else {
31                 return num.toString();
32             }
33         }
34         /*  * 功能:将y进制的数值,转换为x进制的数值 * 参数:第1个参数表示欲转换的数值;第2个参数表示欲转换的进制;第3个参数可选,表示当前的进制数,如不写则为10 * 返回值:返回转换后的字符串 */GUID.prototype.hexadecimal = function (num, x, y) {
35             if (y != undefined) { return parseInt(num.toString(), y).toString(x); }
36             else { return parseInt(num.toString()).toString(x); }
37         }
38         /* * 功能:格式化32位的字符串为GUID模式的字符串 * 参数:第1个参数表示32位的字符串 * 返回值:标准GUID格式的字符串 */
39         GUID.prototype.formatGUID = function (guidStr) {
40             var str1 = guidStr.slice(0, 8) + '-', str2 = guidStr.slice(8, 12) + '-', str3 = guidStr.slice(12, 16) + '-', str4 = guidStr.slice(16, 20) + '-', str5 = guidStr.slice(20);
41             return str1 + str2 + str3 + str4 + str5;
42         }
43     }
44 }

使用

1 var guid = new GUID();
2 
3 alert(guid.newGUID());

猜你喜欢

转载自www.cnblogs.com/CoffeeEddy/p/9037854.html