Skip to content
Advertisement

Regex to check whether string starts with, ignoring case differences

I need to check whether a word starts with a particular substring ignoring the case differences. I have been doing this check using the following regex search pattern but that does not help when there is difference in case across the strings.

my case sensitive way:

var searchPattern = new RegExp('^' + query);
if (searchPattern.test(stringToCheck)) {}

Advertisement

Answer

Pass the i modifier as second argument:

new RegExp('^' + query, 'i');

Have a look at the documentation for more information.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement