Use toUpperCase() or toLowerCase() to standardise your string before testing it.
Use toUpperCase() or toLowerCase() to standardise your string before testing it.
One option is to convert both of them to either lowercase or uppercase:
"Session".toLowerCase().startsWith("sEsSi".toLowerCase());
This is wrong. See: https://stackoverflow.com/a/15518878/14731
Another option is to use String#regionMatches() method, which takes a boolean argument stating whether to do case-sensitive matching or not. You can use it like this:
String haystack = "Session";
String needle = "sEsSi";
System.out.println(haystack.regionMatches(true, 0, needle, 0, needle.length())); // true
It checks whether the region of needle from index 0 till length 5 is present in haystack starting from index 0 till length 5 or not. The first argument is true, means it will do case-insensitive matching.
And if only you are a big fan of Regex, you can do something like this:
System.out.println(haystack.matches("(?i)" + Pattern.quote(needle) + ".*"));
(?i) embedded flag is for ignore case matching.
Pass 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.