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-1/

Text Pattern Matcher

Build a utility that allows users to search for literal text patterns within strings, even when the search terms contain special characters that would normally have meaning in regular expressions.

Requirements

Pattern Matching

Implement a function that takes a search term (which may contain special characters like ., *, ?, $, etc.) and a target text, then returns whether the literal search term appears in the target text.

  • Accept any string as a search term, including strings with regex special characters
  • Perform case-sensitive literal matching (the search term should be found exactly as written)
  • Return a boolean indicating whether the search term was found in the target text
  • Handle edge cases like empty strings, special characters, and Unicode characters (including emojis)

Character Class Pattern Builder

Implement a function that builds a regex character class from a list of literal strings. This is useful for matching any one of several literal patterns, even when those patterns contain special regex characters.

  • Accept an array of strings that may contain special regex characters or hyphens
  • Build a valid regex pattern that matches any of the provided strings when used in a character class context
  • Return a string representing the regex pattern
  • Ensure hyphens are properly escaped to avoid creating unintended character ranges

Implementation

@generates

API

/**
 * Searches for a literal text pattern within a target string.
 *
 * @param {string} searchTerm - The literal text to search for (may contain special characters)
 * @param {string} targetText - The text to search within
 * @returns {boolean} True if the search term is found in the target text
 */
function findLiteralPattern(searchTerm, targetText) {
  // IMPLEMENTATION HERE
}

/**
 * Builds a regex character class pattern from an array of literal strings.
 *
 * @param {string[]} patterns - Array of literal strings to match (may contain special characters)
 * @returns {string} A regex pattern string for use in a character class
 */
function buildCharacterClassPattern(patterns) {
  // IMPLEMENTATION HERE
}

module.exports = {
  findLiteralPattern,
  buildCharacterClassPattern
};

Test Cases

  • Searching for "app.js" in "The file app.js contains code" returns true @test
  • Searching for "What?" in "What? I don't understand" returns true @test
  • Searching for "$100" in "Price: $100.00" returns true @test
  • Searching for "🦄" (emoji) in "I love 🦄 unicorns" returns true @test
  • Building a character class pattern from ["a-z", "0-9"] properly escapes hyphens @test
  • Building a character class pattern from ["$", "*", "."] creates a valid regex @test

Dependencies { .dependencies }

escape-string-regexp { .dependency }

Provides safe escaping of special characters for use in regular expressions.

Install with Tessl CLI

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

tile.json