Modules@robinpath/regex
regex

@robinpath/regex

0.1.1Node.jsPublic

Regular 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.test to perform this operation
  • Find the first match of a pattern in a string -- Use regex.match to perform this operation
  • Find all matches of a pattern in a string -- Use regex.matchAll to perform this operation
  • Replace matches of a pattern in a string -- Use regex.replace to perform this operation
  • Split a string by a regular expression pattern -- Use regex.split to perform this operation

Quick Reference

FunctionDescriptionReturns
testTest if a string matches a regular expression patternTrue if the string matches the pattern, false otherwise
matchFind the first match of a pattern in a stringThe first matching substring, or null if no match
matchAllFind all matches of a pattern in a stringArray of all matching substrings
replaceReplace matches of a pattern in a stringThe string with matches replaced
splitSplit a string by a regular expression patternArray of substrings split by the pattern
captureExtract capture groups from the first match of a patternArray of captured group strings, or null if no match
escapeEscape special regular expression characters in a stringThe 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"
ParameterTypeRequiredDescription
stringstringYesThe string to test against the pattern
patternstringYesThe regular expression pattern
flagsstringNoRegex 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+"
ParameterTypeRequiredDescription
stringstringYesThe string to search
patternstringYesThe regular expression pattern
flagsstringNoRegex 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+"
ParameterTypeRequiredDescription
stringstringYesThe string to search
patternstringYesThe regular expression pattern
flagsstringNoRegex 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"
ParameterTypeRequiredDescription
stringstringYesThe string to perform replacements on
patternstringYesThe regular expression pattern to match
replacementstringYesThe replacement string
flagsstringNoRegex 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+"
ParameterTypeRequiredDescription
stringstringYesThe string to split
patternstringYesThe regular expression pattern to split on
flagsstringNoRegex 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})"
ParameterTypeRequiredDescription
stringstringYesThe string to search
patternstringYesThe regular expression pattern with capture groups
flagsstringNoRegex 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)"
ParameterTypeRequiredDescription
stringstringYesThe string to escape

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(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)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/regex

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.5 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities