let str = "aba"
需求:去除str中"b"字符。
两种方法:replace() , split()
replace:
str = str.replace("b","") //参数1:要替换的字符串,参数2:想要替换成的字符串。
console.log(str) // aaa
split:
第一步分割: str = str.split("b") //参数:指定分割位置的字符串。
console.log(str) //['a','a']
第二步合成:str = str.join("") //将数组元素拼接。
console.log(str) // aa
需求二:去除str中的"a"字符
方法一: replace和indexof和while配合解决:
while(str.indexof("a")!==-1){
str = str.replace("a")
}
console.log(str) // "b"
方法二:正则表达式筛选。
str = str.replace(/a/g,"")
console.log(str) // "b"