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 extendingBaseOperator:
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
- Basic Contains
- Dynamic Field Comparison
- Email Domain Validation
- With Rule Helpers
Common Use Cases
Search Functionality
Search Functionality
Content Filtering
Content Filtering
Category Matching
Category Matching
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
- Basic StartsWith
- Dynamic Prefix
- ID Prefix Validation
Common Use Cases
URL Protocol Validation
URL Protocol Validation
File Path Validation
File Path Validation
Phone Number Format
Phone Number Format
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
- Basic EndsWith
- Dynamic Suffix
- File Type Validation
Common Use Cases
File Upload Validation
File Upload Validation
Email Domain Restriction
Email Domain Restriction
URL Path Matching
URL Path Matching
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
- Email Validation
- Phone Number Validation
- Case-Insensitive Search
- Dynamic Patterns
- Password Strength
Common Use Cases
URL Validation
URL Validation
Credit Card Validation
Credit Card Validation
Date Format Validation
Date Format Validation
Username Validation
Username Validation
Performance: Regex patterns are cached automatically for better performance when evaluating the same pattern multiple times.
Error Handling
Common Errors
Non-String Operands
Non-String Operands
Invalid Regex Pattern
Invalid Regex Pattern
Missing Arguments
Missing Arguments
Undefined Field Access
Undefined Field Access
Error Recovery
Type Coercion
String operators support automatic type coercion in loose mode:Strict Mode
Related Operators
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
