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 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.
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.
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.
@generates
/**
* 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
};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