使用场景:

image.png

只截取一个

  1. let str = '我是一只小猪佩奇'
  2. let middle='***'
  3. function filter(str,middle) {
  4. return str[0]+middle+str[str.length-1]
  5. }
  6. console.log(filter(str,middle));


截取指定的个数

  1. let str = '我是一只小猪佩奇'
  2. let m = '***'
  3. function forMate (str, start = 1, end = 1, middle = '***') {
  4. const startStr = str.slice(0,start)
  5. const endStr = str.slice(str.length-end,str.length)
  6. return startStr + middle + endStr
  7. }
  8. console.log(forMate(str,2,3,m)); //我是***小青龙