Pass the i modifier as second argument:
new RegExp('^' + query, 'i');
Have a look at the documentation for more information.
Answer from Felix Kling on Stack OverflowPass the i modifier as second argument:
new RegExp('^' + query, 'i');
Have a look at the documentation for more information.
You don't need a regular expression at all, just compare the strings:
if (stringToCheck.substr(0, query.length).toUpperCase() == query.toUpperCase())
Demo: http://jsfiddle.net/Guffa/AMD7V/
This also handles cases where you would need to escape characters to make the RegExp solution work, for example if query="4*5?" which would always match everything otherwise.
If you care about case sensitivity then you could use a regex
var isValidImage = /^image/i.test(inputType);
If not then you can just use startsWith, but keep in mind browser compatibility. You might need a Polyfill.
It is simpler to just use the regex which is compatible everywhere.
I know this is years later but just wanted to comment that using a regex has, surprisingly noticeable better performance (~+50%) compared to startsWith or indexOf. I'm guessing the regex engine is highly optimized and probably just passes pointers to c code to actually execute. In most cases it probably doesnt matter but if you're iterating over large data sets checking for matches then use //.test. Also I didnt test in node so the performance may be different but I did test many different browsers.
Here is a very simple jsbench comparison that you can run in your browser: https://jsbench.me/09lqlgolrr/1