Skip to main content

replace

$replace

Replaces occurrences of a pattern in a string.

  • Pattern may be a string (replaced globally via replaceAll) or a RegExp (flags control global).
  • Replacement must be a string.

Parameters

  • First positional: pattern (string or RegExp, required)
  • Second positional: replacement (string, required)

Example

// Replace all underscores with hyphens
new Schema('string').transformer({$replace: ['_', '-']})
// 'hello_world' → 'hello-world'

// Strip all non-digit characters using a RegExp
new Schema('string').normalizer({$replace: [/\D+/g, '']})
// '+1 (800) 555-1234' → '18005551234'

// Redact sensitive patterns
new Schema('string').transformer({$replace: [/\b\d{4}-\d{4}-\d{4}-\d{4}\b/g, '[REDACTED]']})