使用场景:
只截取一个
let str = '我是一只小猪佩奇'
let middle='***'
function filter(str,middle) {
return str[0]+middle+str[str.length-1]
}
console.log(filter(str,middle));
截取指定的个数
let str = '我是一只小猪佩奇'
let m = '***'
function forMate (str, start = 1, end = 1, middle = '***') {
const startStr = str.slice(0,start)
const endStr = str.slice(str.length-end,str.length)
return startStr + middle + endStr
}
console.log(forMate(str,2,3,m)); //我是***小青龙