javascript - 识别字符串中的特定字符(为什么我的代码不起作用?)
问题描述
所以我正在尝试在 JavaScript 中搜索字符串,我的代码如下所示:
var countChar = function(n) {
let checkPhrase = n;
for (let i = 0; i < (checkPhrase.length); i++) {
if ((checkPhrase.indexOf("_") > -1) == true) {
console.log(checkPhrase[i])
}
};
};
countChar("_.");
当我运行代码时,输出是“_”和“。” 为什么?我的 if 语句只能在 checkPhrase === 0 的索引时为真
解决方案
推荐阅读
- docker - 在容器上工作时,Testcafe 没有向 slack 报告
- swift - 以全分辨率在控制器之间传递图像
- c++builder - 关闭具有浮动组件的表单时访问冲突
- javascript - 如何处理 Laravel 到 Vue.js 的布尔值
- python - 注释 Django 查询集不使用向后外键返回注释。使用geodjango
- php - 使用非管理员选择器和重定向组合时,无法保存 Wordpress 函数.php
- amazon-dynamodb - dynamodb 从 GSI 查询多个值
- php - woocommerce 不显示单品描述
- sql-server - 将从 Oracle 数据库实例转储的数据恢复到 SQL Server 数据库实例?
- python - 叶图例阈值在更改值时抛出错误,灰色