<!DOCTYPE html><html lang="UTF-8"><head><meta charset="UTF-8"><title>查找字符所在的位置</title></head><body><script type="text/javascript">let str ='He unfolded the map and set it on the floor.';let arr =[];//索引,找到第一个e,返回其所在的索引值,这里是1//如果找不到则返回-1let pos = str.indexOf('e');
console.log(pos);//如果我们想查找所有的e,那么如下while(pos >-1){
arr.push(pos);//第二个参数表示从哪个位置开始索引,这里指定上次找到的位置的下一个
pos = str.indexOf('e', pos +1);}//输出所有的索引值
console.log(arr);</script></body></html>