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 strings. The utility must validate inputs properly to prevent runtime errors.
The pattern matcher must validate all inputs before processing them.
"hello" returns a valid pattern that can be used in RegExp @testnull to the pattern creator throws a TypeError @testundefined to the pattern creator throws a TypeError @test123 to the pattern creator throws a TypeError @test{} to the pattern creator throws a TypeError @testOnce validated, the utility creates patterns that match the input string literally.
"test" successfully matches the string "test" in a larger text @test"$100" successfully matches "$100" without treating $ as a regex metacharacter @test@generates
/**
* Creates a safe regex pattern from a user-provided string.
* Validates that the input is a string and throws TypeError for invalid inputs.
*
* @param {string} searchString - The string to convert into a safe pattern
* @returns {string} A string that can be safely used in a RegExp constructor
* @throws {TypeError} When the input is not a string
*/
function createSafePattern(searchString) {
// IMPLEMENTATION HERE
}
module.exports = { createSafePattern };Provides functionality for escaping special characters in strings for safe use in regular expressions.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-escape-string-regexp@5.0.0