js实现字符串中间替换成特殊字符***

技术 · 2023-10-06

使用场景如下图:

只截取一个:

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)); //我是***小青龙
javascript
Theme Jasmine by Kent Liao