Skip to content

Incomplete escaping of backslashes in regex input field #253

@johnson86tw

Description

@johnson86tw

Description

The regex input field in ExtractFields.tsx has incomplete character escaping that can corrupt regex patterns containing backslashes.

Current Implementation

Display (line 771):

value={part.regexDef?.replace(/\r/g, '\\r').replace(/\n/g, '\\n')}

Input handling (lines 775-777):

const rawValue = e.target.value
  .replace(/\\r/g, '\r')
  .replace(/\\n/g, '\n');

Problem

The code only escapes \r and \n characters but doesn't escape backslashes themselves. This causes issues when regex patterns contain:

  • \d (digit matcher)
  • \. (literal dot)
  • \\ (literal backslash)
  • Any other escaped regex characters

Expected Behavior

Backslashes should be escaped when displaying and unescaped when parsing input.

Proposed Fix

// Display: escape backslashes FIRST, then handle \r and \n
value={part.regexDef?.replace(/\\/g, '\\\\').replace(/\r/g, '\\r').replace(/\n/g, '\\n')}

// Input: reverse the process - handle \r and \n FIRST, then unescape backslashes
const rawValue = e.target.value
  .replace(/\\r/g, '\r')
  .replace(/\\n/g, '\n')
  .replace(/\\\\/g, '\\');

Note: The order matters! Backslashes must be escaped first when displaying, and unescaped last when parsing.

Location

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions