CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-escape-string-regexp

Escape RegExp special characters in strings for safe use in regular expressions

Overall
score

100%

Evaluation100%

1.00x

Agent success when using this tile

Overview
Eval results
Files

task.mdevals/scenario-3/

Text Search with Unicode Pattern Safety

Build a text search utility that finds literal text patterns within larger strings, with support for Unicode regex patterns and character classes. The utility must work consistently across different regex engines and handle special characters safely.

Requirements

Your utility should:

  1. Accept a search term and a target text, then return all matches of the literal search term in the target text
  2. Support case-insensitive search as an option
  3. Handle search terms containing special regex characters (like $, ., *, [], etc.) as literal text
  4. Work correctly with Unicode regex patterns (using the /u flag)
  5. Handle search terms containing hyphens without creating unintended character ranges

Constraints

  • The search must treat the search term as literal text, not as a regex pattern
  • The implementation must work with both standard regex and Unicode-aware regex patterns
  • Hyphen characters in the search term must not create character class ranges

Dependencies { .dependencies }

escape-string-regexp { .dependency }

Provides regex character escaping functionality.

Implementation

@generates

API

/**
 * Search for all occurrences of a literal text pattern in a target string
 * @param {string} searchTerm - The literal text to search for
 * @param {string} targetText - The text to search within
 * @param {Object} options - Search options
 * @param {boolean} options.caseInsensitive - Whether to perform case-insensitive search (default: false)
 * @param {boolean} options.unicode - Whether to use Unicode-aware regex (default: false)
 * @returns {string[]} Array of all matches found
 */
export function searchLiteral(searchTerm, targetText, options = {});

Test Cases

  • Given searchTerm "$5.99" and targetText "Price: $5.99 today", returns ["$5.99"] @test
  • Given searchTerm "foo-bar" and targetText "foo-bar is here" with unicode: true, returns ["foo-bar"] @test
  • Given searchTerm "Hello" and targetText "hello HELLO Hello" with caseInsensitive: true, returns ["hello", "HELLO", "Hello"] @test
  • Given searchTerm "a-z" and targetText "Pattern a-z found", returns ["a-z"] and not all characters from a to z @test

Install with Tessl CLI

npx tessl i tessl/npm-escape-string-regexp@5.0.0

tile.json