David Brierton
David Brierton

Reputation: 7397

indexOf to match a phrase and show an alert

I am trying to detect if my msg has "No matching records found" in it to do an if/else statement. I know the message has that phrase in it checking in the console.log('msg', msg);

Can someone please tell me if I have written this line incorrectly?

var hasNoRecords = msg.indexOf(/No matching records found/igm) > -1;

I am trying to show an alert if hasNoRecords finds that phrase in the message. But instead, it is bypassing the message as if it's not in the message.

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

Any help with this would be greatly appreciated!

Upvotes: 0

Views: 241

Answers (1)

Grizzly
Grizzly

Reputation: 5943

I don't believe indexOf accepts regular expressions, unless if you create your own custom function. Furthermore, instead of using a regular expression to ignore capitalization you could use .toLowerCase on the string object to transform that object's value into all lowercase and then search for your value (which also has to be in all lower case).

Your current code could be changed to this:

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

Let me know if this helps.

Upvotes: 3

Related Questions