regex functionsFunctions exposed through regex builtin object.
Note: If your regular expression contains backslashes (
\), you will need to do one of the following:
- Anywhere you would use a
\, use two. For example:"\d+\.\d+"becomes"\\d+\\.\\d+"- Use verbatim strings. For example:
"\d+\.\d+"becomes`\d+\.\d+`
regex.escaperegex.escape <pattern>
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space)
by replacing them with their escape codes.
This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.
pattern: The input string that contains the text to convert.A string of characters with metacharacters converted to their escaped form.
input Try out
{{ "(abc.*)" | regex.escape }}
output
\(abc\.\*\)
regex.matchregex.match <text> <pattern> <options>?
Searches an input string for a substring that matches a regular expression pattern and returns an array with the match occurences.
text: The string to search for a match.pattern: The regular expression pattern to match.options: A string with regex options, that can contain the following option characters (default is null):
- i: Specifies case-insensitive matching.
- m: Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.
- s: Specifies single-line mode. Changes the meaning of the dot . so it matches every character (instead of every character except \n).
- x: Eliminates unescaped white space from the pattern and enables comments marked with #.An array that contains all the match groups. The first group contains the entire match. The other elements contain regex matched groups (..). An empty array returned means no match.
input Try out
{{ "this is a text123" | regex.match `(\w+) a ([a-z]+\d+)` }}
output
["is a text123", "is", "text123"]
Notice that the first element returned in the array is the entire regex match, followed by the regex group matches.
regex.matchesregex.matches <text> <pattern> <options>?
Searches an input string for multiple substrings that matches a regular expression pattern and returns an array with the match occurences.
text: The string to search for a match.pattern: The regular expression pattern to match.options: A string with regex options, that can contain the following option characters (default is null):
- i: Specifies case-insensitive matching.
- m: Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.
- s: Specifies single-line mode. Changes the meaning of the dot . so it matches every character (instead of every character except \n).
- x: Eliminates unescaped white space from the pattern and enables comments marked with #.An array of matches that contains all the match groups. The first group contains the entire match. The other elements contain regex matched groups (..). An empty array returned means no match.
input Try out
{{ "this is a text123" | regex.matches `(\w+)` }}
output
[["this", "this"], ["is", "is"], ["a", "a"], ["text123", "text123"]]
Notice that the first element returned in the sub array is the entire regex match, followed by the regex group matches.
regex.replaceregex.replace <text> <pattern> <replace> <options>?
In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.
text: The string to search for a match.pattern: The regular expression pattern to match.replace: The replacement string.options: A string with regex options, that can contain the following option characters (default is null):
- i: Specifies case-insensitive matching.
- m: Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.
- s: Specifies single-line mode. Changes the meaning of the dot . so it matches every character (instead of every character except \n).
- x: Eliminates unescaped white space from the pattern and enables comments marked with #.A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.
input Try out
{{ "abbbbcccd" | regex.replace "b+c+" "-Yo-" }}
output
a-Yo-d
regex.splitregex.split <text> <pattern> <options>?
Splits an input string into an array of substrings at the positions defined by a regular expression match.
text: The string to split.pattern: The regular expression pattern to match.options: A string with regex options, that can contain the following option characters (default is null):
- i: Specifies case-insensitive matching.
- m: Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.
- s: Specifies single-line mode. Changes the meaning of the dot . so it matches every character (instead of every character except \n).
- x: Eliminates unescaped white space from the pattern and enables comments marked with #.A string array.
input Try out
{{ "a, b , c, d" | regex.split `\s*,\s*` }}
output
["a", "b", "c", "d"]
regex.unescaperegex.unescape <pattern>
Converts any escaped characters in the input string.
pattern: The input string containing the text to convert.A string of characters with any escaped characters converted to their unescaped form.
input Try out
{{ "\\(abc\\.\\*\\)" | regex.unescape }}
output
(abc.*)
Note: This document was automatically generated from the source code using
Scriban.DocGen.