JS中字符串的indexOf、startsWith、endsWith、includes String.prototype.indexOf () 返回值完整说明语法str.indexOf(searchValue, [fromIndex])作用查找第一个匹配子串的起始下标找不到返回 -1返回 0子串出现在字符串最开头/profile/1.jpg.indexOf(/profile) // 0返回大于 0 的数字(1、2、3…)子串出现在字符串中间 / 末尾数字是匹配起始位置索引https://xxx.com/profile/a.png.indexOf(/profile) // 17返回 -1字符串里完全不存在该子串https://xxx.com/img/a.png.indexOf(/profile) // -1indexOf 返回值 vs startsWith /endsWith/includesxxx.indexOf(目标) 0等价于str.startsWith(目标)判断字符串以目标开头let s /profile/1.jpg; s.indexOf(/profile) 0; // true s.startsWith(/profile); // true let s2 https://a/profile; s2.indexOf(/profile) 0; // false s2.startsWith(/profile); // falsexxx.indexOf(目标) -1等价于str.includes(目标)判断字符串包含目标任意位置let s https://a/profile/1.jpg; s.indexOf(/profile) -1; // true s.includes(/profile); // true let s2 /test/1.jpg; s2.indexOf(/profile) -1; // false s2.includes(/profile); // falsexxx.indexOf(目标) -1等价于!str.includes(目标)判断字符串不包含目标s.indexOf(/profile) -1 // 等价 !s.includes(/profile)额外补充endsWith(无对应 indexOf 简洁写法)判断以某字符串结尾indexOf 很难简洁实现直接用 endsWithtest.jpg.endsWith(.jpg); // true