v136 · javascript

RegExp.escape

RegExp.escape is a static method that takes a string and returns an escaped version that may be used as a pattern inside a regular expression. For example, copied from the proposal explainer: ``` const str = prompt("Please enter a string"); const escaped = RegExp.escape(str); const re = new RegExp(escaped, 'g'); // handles reg exp special tokens wit

concepts

  1. RegExp.escape

    RegExp.escape() — the long-missing escape function for safely building regexes from user input. Stops the per-project escape-utility duplication.

  2. Multi-term Highlighter

    The TC39 proposal's headline use case: many user terms joined with | into one alternation. Watch the naive path either explode on syntax or silently overmatch while the escaped path keeps literal terms honest.

  3. Path & Glob Builder

    Convert a user glob (src/**/*.tsx) into a regex, keeping wildcards alive while escaping the literal path segments. Try paths with parens, dots, and dollars and watch them match.

  4. Template Tag Helpers

    Two tagged-template helpers: re\`\` escapes interpolations, and likePattern\`\` turns SQL-LIKE wildcards into regex while escaping every literal byte. Both running live against editable haystacks.

  5. Find & Replace editor

    Type any literal string — dots, parens, $price, version(1.0) — and the editor treats it as a literal search, not a regex. RegExp.escape() converts the Find input before compiling the pattern. Toggle Regex mode to disable auto-escaping. Side-by-side shows what a naive new RegExp(input) does with the same text.

why it shipped

To provide a standard method to escape strings for use inside regular expressions, which is a common use pattern and is error-prone due to a large number of corner cases.

references