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 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.
Your utility should:
$, ., *, [], etc.) as literal text/u flag)Provides regex character escaping functionality.
@generates
/**
* 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 = {});"$5.99" and targetText "Price: $5.99 today", returns ["$5.99"] @test"foo-bar" and targetText "foo-bar is here" with unicode: true, returns ["foo-bar"] @test"Hello" and targetText "hello HELLO Hello" with caseInsensitive: true, returns ["hello", "HELLO", "Hello"] @test"a-z" and targetText "Pattern a-z found", returns ["a-z"] and not all characters from a to z @testInstall with Tessl CLI
npx tessl i tessl/npm-escape-string-regexp@5.0.0