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 Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › startsWith
String.prototype.startsWith() - JavaScript - MDN Web Docs
js · startsWith(searchString) startsWith(searchString, position) searchString · The characters to be searched for at the start of this string. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it or passing undefined causes startsWith() to search for the ...
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › regexp-starts-with
Check Whether a String Starts with a Regexp in JavaScript - Mastering JS
December 24, 2021 - Here's how you can use the RegExp `test()` function to test whether a string starts with a regular expression.
🌐
W3Schools
w3schools.com › jsref › jsref_startswith.asp
JavaScript String startsWith() Method
The startsWith() method returns true if a string starts with a specified string.
🌐
Stack Abuse
stackabuse.com › javascript-check-if-string-starts-with-substring
JavaScript: Check if String Starts with Substring
October 4, 2023 - This is easily achieved either through the startsWith() method, or regular expressions.
🌐
Ultimate Courses
ultimatecourses.com › blog › javascript-string-starts-with-characters
Ways to check if a String starts with specific characters - Ultimate Courses
February 3, 2021 - This is basically the same as doing new RegExp('^' + char) but is obviously more awesome, as who likes concatenating strings anymore? Finally, we could also “hard code” our Regular Expression and use it against .test() (which returns a Boolean, by the way): const word = 'JavaScript'; const char = 'J'; /^J/.test(word) // true · But you can see how this would be less appealing. All-in-all, you know startsWith is the way to go, so what’re you waiting for?
🌐
Programiz
programiz.com › javascript › examples › string-start-end
JavaScript Program to Check Whether a String Starts and Ends With Certain Characters
In the above program, a regular expression (RegEx) is used with the test() method to check if the string starts with S and ends with G.
🌐
JavaScript.info
javascript.info › tutorial › regular expressions
Anchors: string start ^ and end $
In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end).
Find elsewhere
🌐
DEV Community
dev.to › suni › is-startswith-faster-than-regexp-4mig
Is startsWith Faster Than RegExp? - DEV Community
July 4, 2023 - $ node startsWith.js loop times: 1000 startsWith: 0.086ms reg: 0.148ms startsWith: 0.033ms reg: 0.08ms startsWith: 0.113ms reg: 0.077ms startsWith: 0.048ms reg: 0.063ms startsWith: 0.032ms reg: 0.072ms startsWith: 0.027ms reg: 0.065ms startsWith: 0.03ms reg: 0.065ms startsWith: 0.026ms reg: 0.065ms startsWith: 0.03ms reg: 0.062ms startsWith: 0.036ms reg: 0.459ms
🌐
Medium
medium.com › dailyjs › string-startswith-method-in-javascript-b12ec998eb54
String startsWith() Method in JavaScript | by Samantha Ming | DailyJS | Medium
August 8, 2019 - const name = 'Samantha Ming';name.startsWith('S'); // true name.startsWith('M'); // false ... Frontend Developer sharing weekly JS, HTML, CSS code tidbits🔥 Discover them all on samanthaming.com 💛
🌐
DEV Community
dev.to › onlinemsr › -top-5-javascript-startswith-alternative-methods-with-examples-48mm
Top 5 JavaScript startsWith() Alternative Methods With Examples - DEV Community
October 3, 2023 - The startsWith() method is the most efficient and readable way to check if a string begins with a certain value. Other methods, such as slicing, regex, or indexOf, perform worse than startsWith() in terms of performance and clarity.
🌐
MSR
rajamsr.com › home › top 5 javascript startswith() alternative methods
Top 5 JavaScript startsWith() Alternative Methods | MSR - Web Dev Simplified
January 28, 2024 - If you are working with strings in JavaScript, you might have encountered situations where you need to check if a string starts with a certain pattern, such as a prefix, a substring, or a regular expression. The built-in startsWith() method can only check for exact matches of prefixes, which can be limiting in some cases.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.startswith()
JavaScript String startsWith() Method
November 3, 2024 - The startsWith() method throws a TypeError if the searchString is a regular expression.
🌐
GitHub
gist.github.com › dai-shi › 4950506
benchmark of String.startsWith equivalents in Node.js · GitHub
indexOf 113,625 ops/sec ±3.51% (78 runs sampled) startsWith 22,522,847 ops/sec ±0.20% (94 runs sampled) lastIndexOf 19,520,195 ops/sec ±0.41% (92 runs sampled) substring 1,368,227,502 ops/sec ±0.07% (95 runs sampled) slice 1,360,307,286 ops/sec ±0.07% (93 runs sampled) regex 26,319,865 ops/sec ±1.17% (91 runs sampled) comp regex 19,868,632 ops/sec ±3.01% (87 runs sampled) Fastest is substring · Added str.startsWith() and tested on Node v12.16.2. The edited code is available HERE ... $ time node starts-with.js indexOf: startsWith: lastIndexOf: substring: slice: regex: compiled regex: >Fastest is node starts-with.js 0.10s user 0.01s system 99% cpu 0.117 total
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 4797 › 1 › js-regex-vs-startswith-vs-indexof
Benchmark: JS Regex vs .startsWith vs .indexOf - MeasureThat.net
startsWith · 969/s · endsWith · 947/s · regex start · 570/s · regex end · 357/s · View exact numbers · LLMs can make mistakes. Check important info. JS Regex vs .startsWith vs .indexOf simpler · JavaScript: Regex vs .startsWith vs .indexOf · JS Regex vs .startsWith ·
🌐
SamanthaMing
samanthaming.com › tidbits › 67-es6-startswith-method
String startsWith() Method in JavaScript | SamanthaMing.com
const lunch = '🥗 🥪 🍮'; // Old Way lunch.indexOf('🥗') === 0; // true // ✅ ES6 Way lunch.startsWith('🥗'); // true
🌐
SDKLABS
javascript.ac › en › reference › string-startswith
JavaScript.ac - Learn JavaScript & Go
Passing a regular expression: startsWith() throws a TypeError for regex arguments rather than converting them.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Match Beginning String Patterns1 - Curriculum Help - The freeCodeCamp Forum
July 18, 2018 - Tell us what’s happening: My exercise do more that: Your regex should search for “Cal” with a capital letter. Can you help how to do it? Your code so far let rickyAndCal = "Cal and Ricky both like racing."; let calRegex = /and Ricky both like racing/; // Change this line let result = calRegex.test(rickyAndCal); Your browser information: User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.