Regexp.match.length returns NULL if not found

I have a JS regexp.

var t1 = str.match(/[h1]/g).length;

If str contains the word [h1] it works fine else it shows an error!
How to solve the problem?

Answer

var t1 = (str.match(/[h1]/g)||[]).length;

Leave a Reply

Your email address will not be published. Required fields are marked *