Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript indexOf以匹配短语并显示警报_Javascript_Jquery - Fatal编程技术网

Javascript indexOf以匹配短语并显示警报

Javascript indexOf以匹配短语并显示警报,javascript,jquery,Javascript,Jquery,我试图检测我的msg中是否有“未找到匹配记录”,以执行if/else语句。我知道在console.log('msg',msg')中检查消息时,消息中有该短语 有人能告诉我这行写错了吗 var hasNoRecords = msg.indexOf(/No matching records found/igm) > -1; 如果hasNoRecords在消息中找到该短语,我将尝试显示警报。但是相反,它绕过了消息,就好像它不在消息中一样 if (hasNoRecords) { al

我试图检测我的msg中是否有
“未找到匹配记录”
,以执行
if/else
语句。我知道在
console.log('msg',msg')中检查消息时,消息中有该短语

有人能告诉我这行写错了吗

var hasNoRecords = msg.indexOf(/No matching records found/igm) > -1;
如果
hasNoRecords
在消息中找到该短语,我将尝试显示警报。但是相反,它绕过了消息,就好像它不在消息中一样

if (hasNoRecords) {
      alert('There are no records found');
    } else {
      $('#pdfiframe').html(msg);
      document.getElementById('pdfiframe').contentWindow.print();
      console.log('hasNoRecords Failed');
    }

在此方面的任何帮助都将不胜感激

我不相信
indexOf
接受正则表达式,除非您创建自己的自定义函数。此外,您可以在string对象上使用
.toLowerCase
将该对象的值转换为所有小写,然后搜索您的值(也必须是所有小写)

您当前的代码可以更改为:

var hasNoRecords = msg.toLowerCase().indexOf("no matching records found") > -1;

让我知道这是否有用。

我不相信
indexOf
接受正则表达式,除非您创建自己的自定义函数。此外,您可以在string对象上使用
.toLowerCase
将该对象的值转换为所有小写,然后搜索您的值(也必须是所有小写)

您当前的代码可以更改为:

var hasNoRecords = msg.toLowerCase().indexOf("no matching records found") > -1;

让我知道这是否有帮助。

当您使用
var hasNoRecords=msg.indexOf(/No-matching records found/igm)>-1执行代码时控制台中出现了什么错误?没有错误all@PaulStrobach为什么使用正则表达式忽略大小写?为什么不
msg.toLowerCase().indexOf(“找不到匹配的记录”)
@GTown编码器总是会失败……)@Keith lol抱歉,当您使用
var hasNoRecords=msg.indexOf(/未找到匹配记录/igm)>-1执行代码时,将
N
更改为小写控制台中出现了什么错误?没有错误all@PaulStrobach为什么使用正则表达式忽略大小写?为什么不
msg.toLowerCase().indexOf(“找不到匹配的记录”)
@GTown编码器总是会失败……)@Keith lol抱歉,将
N
更改为小写。您的HTML不应该做任何事情。。您关心的是
msg
中的值是什么。请把它贴出来,不客气!只需将此答案标记为已接受,即可将此问题视为已回答!您的HTML不应该与此相关。。您关心的是
msg
中的值是什么。请把它贴出来,不客气!只需将此答案标记为已接受,即可将此问题视为已回答!