Skip to content

Commit

Permalink
Update files/en-us/web/javascript/reference/global_objects/regexp/esc…
Browse files Browse the repository at this point in the history
…ape/index.md

Co-authored-by: Hamish Willee <[email protected]>
  • Loading branch information
Josh-Cena and hamishwillee authored Nov 29, 2024
1 parent b5b5eb1 commit 53d173f
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ browser-compat: javascript.builtins.RegExp.escape

The **`RegExp.escape()`** static method [escapes](/en-US/docs/Web/JavaScript/Reference/Regular_expressions#escape_sequences) any potential regex syntax characters in a string, and returns a new string that can be safely used as a [literal](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Literal_character) pattern for the {{jsxref("RegExp/RegExp", "RegExp()")}} constructor.

When dynamically creating a {{jsxref("RegExp")}} with user-provided content, always consider using this function to sanitize the input, unless the input is actually intended to contain regex syntax. In addition, don't try to re-implement its functionality by, for example, using {{jsxref("String.prototype.replaceAll()")}} to insert a `\` before all syntax characters, because `RegExp.escape()` can handle more edge cases and use the right escape sequence that doesn't cause syntax errors in certain contexts.
When dynamically creating a {{jsxref("RegExp")}} with user-provided content, consider using this function to sanitize the input (unless the input is actually intended to contain regex syntax). In addition, don't try to re-implement its functionality by, for example, using {{jsxref("String.prototype.replaceAll()")}} to insert a `\` before all syntax characters. `RegExp.escape()` is designed to use escape sequences that work in many more edge cases/contexts than hand-crafted code is likely to achieve.

## Syntax

Expand Down

0 comments on commit 53d173f

Please sign in to comment.