@robinpath/regex
0.1.1Node.jsPublicRegular expression operations for pattern matching, searching, and replacing
Regex
Regular expression operations for pattern matching, searching, and replacing
Package: @robinpath/regex | Category: Utility | Type: Utility
Authentication
No authentication required. All functions are available immediately.
Use Cases
Use the regex module when you need to:
- Test if a string matches a regular expression pattern -- Use
regex.testto perform this operation - Find the first match of a pattern in a string -- Use
regex.matchto perform this operation - Find all matches of a pattern in a string -- Use
regex.matchAllto perform this operation - Replace matches of a pattern in a string -- Use
regex.replaceto perform this operation - Split a string by a regular expression pattern -- Use
regex.splitto perform this operation
Quick Reference
| Function | Description | Returns |
|---|---|---|
test | Test if a string matches a regular expression pattern | True if the string matches the pattern, false otherwise |
match | Find the first match of a pattern in a string | The first matching substring, or null if no match |
matchAll | Find all matches of a pattern in a string | Array of all matching substrings |
replace | Replace matches of a pattern in a string | The string with matches replaced |
split | Split a string by a regular expression pattern | Array of substrings split by the pattern |
capture | Extract capture groups from the first match of a pattern | Array of captured group strings, or null if no match |
escape | Escape special regular expression characters in a string | The string with special regex characters escaped |
Functions
test
Test if a string matches a regular expression pattern
Module: regex | Returns: boolean -- True if the string matches the pattern, false otherwise
regex.test "hello world" "^hello"
| Parameter | Type | Required | Description |
|---|---|---|---|
string | string | Yes | The string to test against the pattern |
pattern | string | Yes | The regular expression pattern |
flags | string | No | Regex flags (e.g. "i" for case-insensitive, "m" for multiline) |
match
Find the first match of a pattern in a string
Module: regex | Returns: string -- The first matching substring, or null if no match
regex.match "abc 123 def 456" "\d+"
| Parameter | Type | Required | Description |
|---|---|---|---|
string | string | Yes | The string to search |
pattern | string | Yes | The regular expression pattern |
flags | string | No | Regex flags (e.g. "i" for case-insensitive, "m" for multiline) |
matchAll
Find all matches of a pattern in a string
Module: regex | Returns: array -- Array of all matching substrings
regex.matchAll "abc 123 def 456" "\d+"
| Parameter | Type | Required | Description |
|---|---|---|---|
string | string | Yes | The string to search |
pattern | string | Yes | The regular expression pattern |
flags | string | No | Regex flags (e.g. "i" for case-insensitive, "m" for multiline). The "g" flag is automatically added. |
replace
Replace matches of a pattern in a string
Module: regex | Returns: string -- The string with matches replaced
regex.replace "abc 123 def 456" "\d+" "X"
| Parameter | Type | Required | Description |
|---|---|---|---|
string | string | Yes | The string to perform replacements on |
pattern | string | Yes | The regular expression pattern to match |
replacement | string | Yes | The replacement string |
flags | string | No | Regex flags (default: "g" for global replace) |
split
Split a string by a regular expression pattern
Module: regex | Returns: array -- Array of substrings split by the pattern
regex.split "hello world foo" "\s+"
| Parameter | Type | Required | Description |
|---|---|---|---|
string | string | Yes | The string to split |
pattern | string | Yes | The regular expression pattern to split on |
flags | string | No | Regex flags (e.g. "i" for case-insensitive) |
capture
Extract capture groups from the first match of a pattern
Module: regex | Returns: array -- Array of captured group strings, or null if no match
regex.capture "2024-01-15" "(\d{4})-(\d{2})-(\d{2})"
| Parameter | Type | Required | Description |
|---|---|---|---|
string | string | Yes | The string to search |
pattern | string | Yes | The regular expression pattern with capture groups |
flags | string | No | Regex flags (e.g. "i" for case-insensitive) |
escape
Escape special regular expression characters in a string
Module: regex | Returns: string -- The string with special regex characters escaped
regex.escape "price is $9.99 (USD)"
| Parameter | Type | Required | Description |
|---|---|---|---|
string | string | Yes | The string to escape |
Error Handling
All functions throw on failure. Common errors:
| Error | Cause |
|---|---|
| (standard errors) | Check function parameters and authentication |
@desc "Test and validate result"
do
set $result as regex.test "hello world" "^hello"
if $result != null
print "Success"
else
print "No result"
end
enddo
Recipes
1. Multi-step Regex workflow
Chain multiple regex operations together.
@desc "Test, match, and more"
do
set $r_test as regex.test "hello world" "^hello"
set $r_match as regex.match "abc 123 def 456" "\d+"
set $r_matchAll as regex.matchAll "abc 123 def 456" "\d+"
print "All operations complete"
enddo
2. Safe test with validation
Check results before proceeding.
@desc "Test and validate result"
do
set $result as regex.test "hello world" "^hello"
if $result != null
print "Success: " + $result
else
print "Operation returned no data"
end
enddo
Related Modules
- json -- JSON module for complementary functionality
Versions (1)
| Version | Tag | Published |
|---|---|---|
| 0.1.1 | latest | 1 months ago |
Related Modules
@robinpathv0.1.4
SMTP email sending and address parsing for RobinPath
hash
JS@robinpathv0.1.3
Cryptographic hashing utilities: MD5, SHA family, HMAC, CRC32, file hashing, UUID v5 generation, secure random bytes, and content fingerprinting
csv
JS@robinpathv0.1.2
Parse and stringify CSV data
apollo
JS@robinpathv0.1.2
Apollo module for RobinPath.
$ robinpath add @robinpath/regex
