// 声明排查函数 function paicha(str, value) { var strIndex = str.indexOf(value); // 将value在字符串str中的位置赋值给变量strIndex if (strIndex === -1) { // 判断strIndex是否等于-1,相等说明在str中不存在value console.log("不存在敏感字"); } else { for (var i = 0; i < str.length; i++) { // 遍历字符串 var strKey = str.charAt(i); // 将取得的str的每个字符赋值给strKey if (strKey === value) { // 判断取得的字符是否与希望匹配的字符相等 console.log("敏感字" + value + "在" + i + "位置被发现"); // 输出对应的i,即为value在字符串中位置 } } console.log("敏感字" + value + "首次出现的位置是" + strIndex); } } var strObj = prompt("请输入搜索内容:"); // 执行排查函数 paicha(strObj, "草");