Skip to content

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Dec 27, 2025

Replace Python's re module with the internal microjs.regex engine for all String methods that use regex patterns. This ensures:

  • ReDoS protection via step limits and timeout polling
  • Consistent behavior with the rest of the regex implementation
  • Proper sandboxing when executing untrusted code

Implementation details:

  • All four methods now use JSRegExp._internal (microjs.regex.RegExp)
  • Creates fresh regex VM for each search to avoid lastIndex issues
  • Catches RegexTimeoutError and converts to TimeLimitError
  • Handles capture groups correctly (capture_count includes group 0)

Added comprehensive tests including:

  • Edge cases (case-insensitive, multiline, zero-width matches)
  • Time limit/ReDoS protection tests with parametrized methods
  • Robust tests for various scenarios (global vs non-global, groups, etc.)

Closes #15

https://gistpreview.github.io/?e0b4931c8962017eee467e6e9cd1d49d/index.html

Replace Python's `re` module with the internal microjs.regex engine for
all String methods that use regex patterns. This ensures:

- ReDoS protection via step limits and timeout polling
- Consistent behavior with the rest of the regex implementation
- Proper sandboxing when executing untrusted code

Implementation details:
- All four methods now use `JSRegExp._internal` (microjs.regex.RegExp)
- Creates fresh regex VM for each search to avoid lastIndex issues
- Catches RegexTimeoutError and converts to TimeLimitError
- Handles capture groups correctly (capture_count includes group 0)

Added comprehensive tests including:
- Edge cases (case-insensitive, multiline, zero-width matches)
- Time limit/ReDoS protection tests with parametrized methods
- Robust tests for various scenarios (global vs non-global, groups, etc.)

Closes #15
Comment on lines +1866 to +1867
# Create fresh regex VM for each search to avoid lastIndex issues
vm_regex = regex_internal._create_vm()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds inefficient, I wonder what the performance hit here is.

@simonw simonw merged commit 4cd53cc into main Dec 27, 2025
10 checks passed
@simonw simonw deleted the claude/add-arrow-function-support-Bf2bS branch December 27, 2025 18:05
simonw added a commit that referenced this pull request Dec 27, 2025
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.

String.match(), .search(), .replace(), .split() should use microjs.regex

3 participants