Herramientas HTML

Gratis Ayudante RegExp.escape()

Genere llamadas RegExp.escape() para escapar caracteres especiales en cadenas para patrones regex dinamicos seguros.

Cargando herramienta...

Qué es Ayudante RegExp.escape()?

RegExp.escape(string) is a static method that returns a new string where every special regular expression character is escaped with a backslash. This makes the string safe to use inside a RegExp constructor when building dynamic patterns from user input. The method is part of Baseline 2025 (Chrome 132+) and replaces error-prone manual escape functions that developers have been writing and copying for decades.

Respuesta rápida

Generate code using RegExp.escape(string) to safely escape special regex characters in user input before building dynamic RegExp patterns. Supports all 14 regex metacharacters. Available in Chrome 132+, Edge, Firefox, Node.js 20+. No more manual escape regex chains.

Limitaciones

  • Not available in environments without Baseline 2025 support. Use a polyfill or the traditional manual replace function for older Node.js versions and legacy browsers.
  • RegExp.escape() only handles string inputs. For RegExp flags or pattern options, you must still build the full pattern string and flags array yourself.
  • The method escapes every special character unconditionally. If you need to build a pattern that mixes literal user input with intentional regex syntax (like wildcards), you must separately handle the intentional syntax.

Cómo usar esta herramienta

  1. Identify where you need to build a RegExp pattern from user-provided or variable input, such as search boxes or filter inputs.
  2. Pass the input string to RegExp.escape() before inserting it into the pattern string for the RegExp constructor.
  3. Use the escaped string in your RegExp pattern: new RegExp(RegExp.escape(userInput), 'gi').
  4. Review the generated code included in the tool output, which shows both the native method and the manual escape function for comparison.

Para qué puedes usarla

  • Build a client-side search filter where user keystrokes are turned into a regex for live filtering — escaping prevents invalid or injected patterns from breaking the regex.
  • Create a find-and-replace UI where the user types a literal string to search for and a replacement. Escaping ensures that characters like . or * are treated as literals.
  • Highlight search terms in text content by constructing a RegExp from user input and wrapping matches in a span — escaping guarantees the pattern is syntactically valid.

Casos de uso

Ejemplos prácticos

Ejemplo

Live search filter

A search input on a product listing page filters items by name. The user types something like .NET or (Premium). RegExp.escape() turns each query into a safe pattern so the search works correctly even with special characters.

Ejemplo

Find and replace in a text editor

A simple browser-based text editor lets users find and replace text. The find string is escaped with RegExp.escape() before building the replacement regex, so searching for $100 or (C) finds literal matches instead of triggering regex syntax.

Errores comunes

  • Forgetting to escape user input and embedding it directly in a regex pattern — this causes syntax errors when the input contains special characters like (, ), or * and breaks the entire regex.
  • Using RegExp.escape() on the entire pattern string that already contains regex metacharacters intentionally — the method escapes everything, so pre-built patterns with quantifiers or groups will lose their special meaning.
  • Assuming RegExp.escape() also handles null or undefined inputs — pass a string value or check the input type before calling to avoid TypeError.

Verificación

  1. Open Chrome 132+ DevTools Console. Call RegExp.escape('hello.world') and verify it returns 'hello\.world'. Call RegExp.escape('test (1) [2]') and confirm the parentheses and brackets are escaped.
  2. Construct new RegExp(RegExp.escape('.*+?'), 'g') and test it against the literal string '.*+?' — verify it matches, confirming the special characters are treated as literals.

FAQ

Preguntas sobre Ayudante RegExp.escape()

What characters does RegExp.escape() escape?

The method escapes the 14 special regex characters: ^ $ \ . * + ? ( ) [ ] { } |. Each is preceded by a backslash in the returned string. For example, RegExp.escape('hello.world') returns 'hello\.world', and RegExp.escape('price $10') returns 'price \$10'.

How does RegExp.escape() compare to the manual escape function developers used before?

The manual approach typically used string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') — a hard-to-read regex that developers often copied without understanding. RegExp.escape() is a standard API with consistent behavior, no regex-in-a-regex, and no edge-case bugs. It is simpler, faster, and maintained by the browser vendor.

Is RegExp.escape() available in Node.js?

RegExp.escape() is a JavaScript language feature, not a Web API, and is available in Node.js 20+ as a core method on the RegExp constructor. No polyfill or npm package is needed. Check your Node.js version for support.

Can I use RegExp.escape() with Unicode or special spaces?

Yes. RegExp.escape() works on any valid JavaScript string including Unicode characters and unusual whitespace. It only escapes the 14 regex metacharacters and leaves all other characters including non-Latin scripts untouched.

Herramientas relacionadas

Más herramientas html

Prueba también

Prueba también