Wildcard Matching Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Wildcard Matching runs in O(n*m) time and O(n*m) space, in JavaScript as in every other language.
- ✓There is no glob matcher in the language. The only built-in engine is RegExp, so the common move is to translate the pattern: escape every regex metacharacter, map '?' to '.' and '*' to '.*', anchor with ^ and $.
- ✓Reviewers reject building a RegExp from user input without escaping, and reject building it inside the loop instead of once outside.
- ✓The JavaScript trap: A JavaScript string is a sequence of UTF-16 code units, so '?' as written above matches one code unit, not one character.
- ✓Uint8Array rows keep the DP in a packed backing store, avoiding the boxed-element and holey-array shapes that push V8's element kind from PACKED_SMI to a slower one.
What Wildcard Matching actually is
Wildcard Matching in JavaScript: what you actually reach for
JavaScript implementation
function isMatch(s, p) {
const n = s.length, m = p.length;
const STAR = 42, QMARK = 63;
let prev = new Uint8Array(m + 1);
let cur = new Uint8Array(m + 1);
prev[0] = 1;
for (let j = 1; j <= m; j++) {
prev[j] = prev[j - 1] && p.charCodeAt(j - 1) === STAR ? 1 : 0;
}
for (let i = 1; i <= n; i++) {
const sc = s.charCodeAt(i - 1);
cur[0] = 0;
for (let j = 1; j <= m; j++) {
const pc = p.charCodeAt(j - 1);
if (pc === STAR) cur[j] = cur[j - 1] | prev[j];
else if (pc === QMARK || pc === sc) cur[j] = prev[j - 1];
else cur[j] = 0;
}
const t = prev; prev = cur; cur = t;
}
return prev[m] === 1;
}
const toRegExp = (p) =>
new RegExp('^' + p.replace(/[.+^${}()|[\]\\]/g, '\\$&')
.replace(/\*/g, '.*').replace(/\?/g, '.') + '$', 's');Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(n*m)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Wildcard Matching?
There is no glob matcher in the language. The only built-in engine is RegExp, so the common move is to translate the pattern: escape every regex metacharacter, map '?' to '.' and '*' to '.*', anchor with ^ and $. There is no escape helper you can rely on across every deployed engine, so people hand-roll str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'). Node has been growing filesystem glob helpers (fs.glob, path.matchesGlob), but they are recent and absent in browsers. On npm the de-facto matchers are picomatch, which powers fast-glob and chokidar, and minimatch, which npm itself uses.
What is the most common JavaScript mistake when implementing Wildcard Matching?
A JavaScript string is a sequence of UTF-16 code units, so '?' as written above matches one code unit, not one character. '\u{1F600}' with pattern '?' fails and only '??' matches it, because that emoji is a surrogate pair and charCodeAt walks code units. Worse, mixing iteration styles desynchronises the DP: for...of walks code points while s.length and s[i] walk code units, so a matcher written half one way and half the other reads past the end of the pattern on the first astral character. If you need character semantics, build the index once with Array.from(s) and drive the DP off that array's length.
How does Wildcard Matching actually perform in JavaScript?
Uint8Array rows keep the DP in a packed backing store, avoiding the boxed-element and holey-array shapes that push V8's element kind from PACKED_SMI to a slower one. charCodeAt returns a number and allocates nothing; s[i] materialises a one-character string that the engine may or may not fold away. The whole matcher is short and monomorphic, so it tiers up quickly, but the first few thousand iterations run in the interpreter, which is why a naive one-shot benchmark of a small pattern reports numbers dominated by warmup rather than by the O(n*m) fill. Time complexity is O(n*m) and space is O(n*m).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Devin, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.