MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / stringMatching

Function stringMatching

src/pkg/utils/utils.ts:463–489  ·  view source on GitHub ↗
(main: string, sub: string)

Source from the content-addressed store, hash-verified

461};
462
463export const stringMatching = (main: string, sub: string): boolean => {
464 // If no wildcards, use simple includes check
465 if (!sub.includes("*") && !sub.includes("?")) {
466 return main.includes(sub);
467 }
468
469 // Escape special regex characters except * and ?
470 const escapeRegex = (str: string) => str.replace(/[-[\]{}()+.,\\^$|#\s]/g, "\\$&");
471
472 // Convert glob pattern to regex
473 let pattern = escapeRegex(sub)
474 .replace(/\*/g, "\\S*") // * matches zero or more non-space characters
475 .replace(/\?/g, "\\S"); // ? matches exactly one non-space character
476
477 // Anchor the pattern to match entire string
478 pattern = `\\b${pattern}\\b`;
479
480 try {
481 // Create regex and test against main string
482 const regex = new RegExp(pattern);
483 return regex.test(main);
484 } catch (e) {
485 console.error(e);
486 // Handle invalid regex patterns
487 return false;
488 }
489};
490
491/**
492 * 将字节数转换为人类可读的格式(B, KB, MB, GB 等)。

Callers 3

utils.test.tsFile · 0.90
searchNameMethod · 0.90
searchCodeMethod · 0.90

Calls 2

escapeRegexFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected