Skip to content

Fix LIKE operator regex escaping for literal special characters#24

Merged
rathboma merged 1 commit intomainfrom
claude/fix-lib-bug-Y5JOZ
Apr 20, 2026
Merged

Fix LIKE operator regex escaping for literal special characters#24
rathboma merged 1 commit intomainfrom
claude/fix-lib-bug-Y5JOZ

Conversation

@rathboma
Copy link
Copy Markdown
Contributor

Summary

Fixed a bug in the LIKE operator implementation where regex metacharacters in SQL patterns were not being properly escaped, causing incorrect matching behavior when patterns contained special characters like dots, parentheses, or dollar signs.

Key Changes

  • Regex escaping in LIKE operator: Modified the LIKE case in compiler.ts to escape JavaScript regex metacharacters (.*+?^${}()|[\]\\) before translating SQL wildcards (% and _). This ensures that literal characters in the pattern are matched exactly, while SQL wildcards continue to work as expected.
  • Comprehensive test coverage: Added a new test suite "LIKE operator regex escaping" with five test cases covering:
    • Dots in patterns (e.g., email addresses)
    • Parentheses in patterns (e.g., book titles)
    • Dollar signs in patterns (e.g., prices)
    • Percent wildcard (%) functionality
    • Underscore wildcard (_) functionality

Implementation Details

The fix changes the pattern compilation order:

  1. First escape all regex metacharacters in the input string
  2. Then replace SQL wildcards (%.*, _.) with their regex equivalents
  3. Finally wrap with anchors and case-insensitive flag

This ensures that a pattern like 'user.name@example.com' correctly matches only that exact string (with the dot treated literally), while 'Jo%' still matches any string starting with "Jo".

https://claude.ai/code/session_01SPmihaVijYGP1zqmkfNUyx

The LIKE-to-regex conversion only translated SQL wildcards (% and _) and
passed every other character through to RegExp verbatim, so regex-significant
characters in the SQL literal produced wrong matches:

- LIKE 'user.name@example.com' matched 'userXname@example.com' (dot is any-char)
- LIKE 'Books (Fiction)' matched 'Books Fiction' (parens became a capture group)
- LIKE 'Price: \$10' compiled to /^Price: \$10\$/i, which matches nothing

Escape JS regex metacharacters in the SQL literal before translating the two
wildcards. Adds unit coverage for the three failure cases plus % and _ to
lock in wildcard semantics against the escape step.
@rathboma rathboma merged commit 05e65ab into main Apr 20, 2026
6 checks passed
@rathboma rathboma deleted the claude/fix-lib-bug-Y5JOZ branch April 20, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants