Use toUpperCase() or toLowerCase() to standardise your string before testing it.
java - How to use `string.startsWith()` method ignoring the case? - Stack Overflow
How to make code in JS case insensitive?
javascript - Regex to check whether string starts with, ignoring case differences - Stack Overflow
Case insensitive using startswith
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.
» npm install case-insensitive
Ok so here’s an example of my code: if (message.content.startsWith(“k beg”)) {
Basically, the code will work but the user must type in exactly “k beg” all lowercase. I want the code to be case insensitive (for example (K bEg), AND the message content should be just the input, so “k beg” shouldn’t trigger when it’s in the middle of a sentence.
One of my major points though is to make it case insensitive, so if anyone can help with just that, I’ll be happy. Thank you.
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.