1. 高亮标记
function markRed (str) {
if (!str) return ""
return str
.replace(/\{/g, '<span class="red-name">')
.replace(/\}/g, '</span>')
.replace('$markName', this.textDesc)
}
或者 动态数据,使用占位符进行匹配
if (this.signText.indexOf('$markName') !== -1) {
this.signText = this.signText.replace('xxxx', this.textDesc)
}
2. 处理日期20220909为 2022-09-09
function formatDate(date) {
return `${date.slice(0, 4)} - ${date.slice(4, 6)} - ${date.slice(6,8)}`
}
3. 时间转时间戳,并比较
const time1 = '2022/03/20 10:59:50'
const time2 = '15:33'
function duriingTime (time1, time2) {
const nowTime = new Date(time1).getTime()
const endTime = new Date(time1.slice(0,11) + `${time2}:00`).getTime()
return nowTime <= endTime
}
# 4. 字符串截取,截取小括号包含的内容
let str = 你好啊(000100)
str.replace(/(([^)]*))/g, “”)