~function(){
<!--
formatTime:时间字符串的格式化处理
@params
template:[string] 我们最后期望获取日期格式的模板
模板规则:{0}->年 {1~5}->月日时分秒
@return
[string]格式化后的时间字符串
-->
function formatTime(template = "{0}年{1}月{2}日 {3}时{4}分{5}秒"){
let timeAry = this.match(/\d+/g)
return template.replace(/\{(\d+)\}/g,(content,$1)=>{
let time = timeAry[$1] || "00"
return time.length < 2 ? "0" + time : time
})
}
<!-- 扩展到内置类String.prototype上 -->
['formatTime'].forEach(item => {
String.prototype[item] = eval(item)
})
}()
let time = '2020-11-9 11:24:55'
console.log(this.formatTime())
~function(){
<!--
queryURLParams:获取URL地址问号后面的参数信息(也可能包含HASH值)
@params
@return
[object]把所有问号参数信息以键值对的方式存储起来并且返回
-->
function queryURLParams(){
let obj = {}
this.replace(/([^?=&#]+)=([^?=&#]+)/g,(...[,$1,$2])=>obj[$1]=$2)
this.replace(/#([^?=&#]+)/g,(...[,$1])=>obj['HASH']=$1)
return obj
}
<!-- 扩展到内置类String.prototype上 -->
['queryURLParams'].forEach(item => {
String.prototype[item] = eval(item)
})
}()
let url = 'http://www.aabbcc.com/?a=1&b=2#c'
console.log(url.queryURLParams())
~function(){
<!--
millimeter:实现大数字的千分符处理
@params
@return
[string]千分符后的字符串
-->
function millimeter(){
return this.replace(/\d{1,3}(?=(\d{3})+$)/g,content => content + ',')
}
<!-- 扩展到内置类String.prototype上 -->
['millimeter'].forEach(item => {
String.prototype[item] = eval(item)
})
}()
let num = '12345678'
把字符串倒过来加
num = num.split('').reverse().join('')
for(let i = 2;i < num.length - 1; i += 4){
let prev = num.substring(0,i+1),
next = num.substring(i+1)
num = prev + ',' + next
}
num = num.split('').reverse().join('')
console.log(num)