1.获取字符串中的元音
getYuan (str) {
let arr = str.match(/[aeiou]/gi)
return arr
}
2.将带#的字符串特殊处理
parseComments (value) {
var exp
exp = /(#|#)((?!#).)*(#|#)/g
value = value.replace(/<[^>]*?>(.*?)/gi, '').replace(/(.*?)<\/[^>]*?>/gi, '').replace(exp, (item) => {
let newVal = '<span class="text-blue">' + item + '</span>'
return newVal
})
return value
}
3.平方每个数
this.squareNum(9119)
squareNum (str) {
let strArr = (str + '').split('')
let content = strArr.map((item) => {
return Number(item) * Number(item)
}).join('')
return content
}
3.
console.log('is2', 'is2'.replace(/1/gi, '')) // 2; 去掉字符串的非数字
console.log('sdf34', 'sdf34'.replace(/d/g, '')) // sdf; 去掉数字
4.数组的深拷贝
(1) slice 方法复制数组
let copy = [1, 2, 3]
let deepCopy = copy.slice()
copy[2] = 4
console.log('copy', copy) // [1,2,4]
console.log('deepCopy', deepCopy) // [1,2,3]
(2) concat 方法复制数组
let copy = [1, 2, 3]
let deepCopy = [].concat(copy)
copy[2] = 4
console.log('copy', copy) // [1,2,4]
console.log('deepCopy', deepCopy) // [1,2,3]
(3) 遍历数组
forEach 或者 for循环
(4) 扩展运算符 方法复制数组
let copy = [1, 2, 3]
let deepCopy = [...copy]
copy[2] = 4
console.log('copy', copy)
console.log('deepCopy', deepCopy)
(5) 转换成json再转换成对象
let copy = [1, 2, 3]
let deepCopy = JSON.parse(JSON.stringify(copy))
copy[2] = 4
console.log('copy', copy)
console.log('deepCopy', deepCopy)
5.对象的深拷贝
(1) for循环
let copy = {
name: 'xiaoyingzi',
sex: 'female',
age: 25
}
let res = {}
for (let i in copy) {
console.log('i=====', i)
res[i] = copy[i]
}
console.log('res', res)
copy.name = 'xiaoyouzi'
console.log('res', res)
(2) 转换成json再转换成对象
- 0-9 ↩
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。