Escape RegExp special characters in strings for safe use in regular expressions
Overall
score
100%
Evaluation — 100%
↑ 1.00xAgent success when using this tile
Build a utility that creates safe regular expression patterns from user-provided search terms. The utility should handle various types of input strings and produce clean, minimal patterns suitable for searching text.
Create a module that:
The output should be a clean string that contains only the minimal necessary transformations to make the search term safe for use in regular expressions.
Create the following files:
searchBuilder.js - Main module with search pattern building logicsearchBuilder.test.js - Test suiteInput:
const term = "app.js";
// Build pattern and use in RegExpExpected behavior: The pattern should match "app.js" but not "appXjs" when used in a RegExp.
Input:
const term = "price: $19.99?";
// Build pattern and use in RegExpExpected behavior:
The pattern should match the exact string "price: $19.99?" and handle the $, ., and ? characters safely.
Input:
const term = "café";
// Build patternExpected behavior: The pattern should match "café" exactly, with Unicode characters preserved unchanged.
Input:
const term = null;
// Attempt to build patternExpected behavior: Should throw a TypeError with a meaningful message about expecting a string.
Provides safe regex pattern generation from strings.
Install with Tessl CLI
npx tessl i tessl/npm-escape-string-regexp@5.0.0