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 exact matches of user-provided search terms within larger text documents, even when the search terms contain special characters.
Your utility must support searching for literal text strings that may contain any characters including those that have special meaning in regular expressions (such as ., *, ?, $, +, [, ], (, ), {, }, |, ^, \, and -).
The search should:
@generates
/**
* Searches for a literal text string within 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.caseSensitive - Whether search is case-sensitive (default: true)
* @returns {boolean} True if the search term is found, false otherwise
*/
function searchText(searchTerm, targetText, options = { caseSensitive: true });
/**
* Finds all matches of a literal text string within 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.caseSensitive - Whether search is case-sensitive (default: true)
* @returns {Array<string>} Array of all matches found
*/
function findAllMatches(searchTerm, targetText, options = { caseSensitive: true });"$100" in "The price is $100 today" with case-sensitive mode returns true @test"file.txt" in "Open file.txt now" with case-sensitive mode returns true @test"Mr. Smith" in "Looking for mr. smith" with case-insensitive mode returns true @test"$.99" in "Items cost $.99, $.99, and $1.00" returns ["$.99", "$.99"] @test"[urgent]" in "Message: [urgent] reply needed" returns true @testProvides string escaping for safe use in regular expressions.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-escape-string-regexp@5.0.0