Skip to main content

Overview

String operators enable powerful text matching and validation capabilities. Rule Engine JS provides four string operators for different text comparison needs:

contains

Check if string contains substring

startsWith

Check if string starts with prefix

endsWith

Check if string ends with suffix

regex

Match against regular expression patterns

Architecture

String operators are implemented across two classes extending BaseOperator: Source Files:
  • String operators: src/operators/string.js
  • Regex operator: src/operators/regex.js
  • Base operator: src/operators/base/BaseOperator.js
  • Type utilities: src/utils/TypeUtils.js
  • Unit tests: tests/unit/operators/string.test.js

Key Features

  • Dynamic field comparison - Compare two fields or field to literal value
  • Case sensitive by default - Exact matching for security
  • Automatic type coercion - Non-string values converted to strings
  • Pattern caching - Regex patterns cached for performance
  • Comprehensive validation - Validates argument count and types

contains - Substring Match

Check if a string contains a substring.

Syntax

Parameters

string
required
The string to search in - field path or literal value
string
required
The substring to search for - field path or literal value
object
Configuration options

Returns

boolean - true if haystack contains needle, false otherwise

Examples

Common Use Cases

Case Sensitivity: contains is case-sensitive by default. “Software” !== “software”

startsWith - Prefix Match

Check if a string starts with a specific prefix.

Syntax

Parameters

string
required
The string to check - field path or literal value
string
required
The prefix to match - field path or literal value
object
Configuration options

Returns

boolean - true if string starts with prefix, false otherwise

Examples

Common Use Cases

endsWith - Suffix Match

Check if a string ends with a specific suffix.

Syntax

Parameters

string
required
The string to check - field path or literal value
string
required
The suffix to match - field path or literal value
object
Configuration options

Returns

boolean - true if string ends with suffix, false otherwise

Examples

Common Use Cases

regex - Regular Expression Match

Match strings against regular expression patterns with support for flags and pattern caching.

Syntax

Parameters

string
required
The text to match against - field path or literal value
string
required
The regular expression pattern - field path or literal value
object
Configuration options

Returns

boolean - true if text matches pattern, false otherwise

Examples

Common Use Cases

Performance: Regex patterns are cached automatically for better performance when evaluating the same pattern multiple times.

Error Handling

Common Errors

Error Recovery

Type Coercion

String operators support automatic type coercion in loose mode:

Strict Mode

Comparison Operators

eq, neq for exact matching

Array Operators

in, notIn for array membership

Special Operators

isNull, isNotNull, between

Logical Operators

and, or, not for combining rules

Numeric Operators

Numeric operations

All Operators

Complete operator reference

API Reference

For complete API documentation: