手机号中间4位变成*星号
let tel = '12345678910'
const reg = /^(\d{3})\d{4}(\d{4})/
tel = tel.replace(reg, "$1****$2") // 123****8910
以abc开头,以def结尾(包含def)
https://blog.csdn.net/qq_32623363/article/details/78808132?utm_source=copy
const reg = /http(.*?(.png|.gif|.jpg|.jpeg))/gi // 匹配以http开头,.png|.gif|.jpg|.jpeg结尾
拓展: 以abc开头,以def结尾(不包含def)
const reg = /http(.*?)(?=(.png|.gif|.jpg|.jpeg))/gi
校验十六进制颜色
const reg = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
reg.test('#99ffa8') // true
reg.test('#369') // true
reg.test('#3698') // false
svg元素path路径平移(简易版,path中含有Q/A的有bug,慎用)
const str = `M100,400 C200,300 300,300 400,400 c500,500 600,500 700,400`
const arr = str.split(/(M | m | z| Z | C | c)/)
console.log(arr);
let nArr = []
let dx = 0;
let dy = 0;
let startPointIdx = 0;
arr.forEach((el, index) => {
if (el) {
if (el.startsWith('m') || el.startsWith('M')) {
startPointIdx = index + 1
}
const tempArr = el.split(' ')
let tempStr = ''
if (index === startPointIdx) {
dx = -Number(tempArr[0])
dy = -Number(tempArr[1])
console.log(dx,dy);
}
tempArr.forEach((ele, idx) => {
if (parseInt(ele)) {
if (idx % 2 === 0) {
// 偶数, x轴
tempStr += ' ' + (Number(tempArr[idx]) + dx) + ' '
} else {
//奇数,y轴
tempStr += ' ' + (Number(tempArr[idx]) + dy) + ' '
}
} else {
console.log(tempArr[idx]);
tempStr += ' ' + tempArr[idx] + ' '
}
})
nArr.push(tempStr)
}
})
let s = nArr.join(' ').replace(/\s+/g, ' ')
console.log(s);