FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

ReplaceStrRegex

This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics.

ReplaceStrRegex( value, pattern, replace, [ first_only ] )
replace_str_regex( value, pattern, replace, [ first_only ] )
ReplaceStrRegex( value, pattern, replace, [ first_only ] )
ReplaceStrRegex( value, pattern, replace, [ first_only ] )
ReplaceStrRegex( value, pattern, replace, [ first_only ] )

Description

The ReplaceStrRegex function returns a string which has either the first or all occurrences of the pattern replaced with the replace string. The pattern conforms to Java regular expression syntax.

Parameters

Parameter Type Definition and Requirements

value

String

The source String to perform replacements within.

pattern

String

The Java regular expression to match against the value string.

replace

String

The new String that replaces the sub-string found by pattern.

first_only

Boolean

Optional - When true, replace only the first occurrence of the pattern. Default is false.

Since Go does not have function overloading or optional arguments, you must use the function f.OnlyFirst() instead of a literal true.

Returns

A String which has either the first or all occurrences of the pattern replaced with the replace string.

Examples

The following query executes an array of independent ReplaceStrRegex operations and returns the results in an Array. The result array position matches the execution array position.

The individual operations are:

  1. The string "One Fish Two Fish" is processed, replacing the pattern "Two" with the string "Blue". The result string "One Fish Blue Fish" is placed in the top position of the result array.

  2. The string "One Fisk Two FisT" is processed, replacing every string matching the pattern "Fis." with the string "Fish". The result string "One Fish Two Fish" is placed in the second position of the result array.

  3. The same string "One Fisk Two FisT" is processed, but because it has the first_only flag set to true, only the first string matching the pattern "Fis." is replaced. The result string "One Fish Two FisT" is placed in the third position of the result array.

client.query(
  [
    q.ReplaceStrRegex('One Fish Two Fish', 'Two', 'Blue'),
    q.ReplaceStrRegex('One Fisk Two FisT', 'Fis.', 'Fish'),
    q.ReplaceStrRegex('One Fisk Two FisT', 'Fis.', 'Fish', true),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ 'One Fish Blue Fish', 'One Fish Two Fish', 'One Fish Two FisT' ]
result = client.query(
  [
    q.replace_str_regex("One Fish Two Fish", "Two", "Blue"),
    q.replace_str_regex("One Fisk Two FisT", "Fis.", "Fish"),
    q.replace_str_regex("One Fisk Two FisT", "Fis.", "Fish", True),
  ]
)
print(result)
["One Fish Blue Fish", "One Fish Two Fish", "One Fish Two FisT"]
result, err := client.Query(
	f.Arr{
		f.ReplaceStrRegex("One Fish Two Fish", "Two", "Blue"),
		f.ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish"),
		f.ReplaceStrRegex(
			"One Fisk Two FisT",
			"Fis.",
			"Fish",
			f.OnlyFirst(),
		),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[One Fish Blue Fish One Fish Two Fish One Fish Two FisT]
try
{
    Value result = await client.Query(
        Arr(
            ReplaceStrRegex("One Fish Two Fish", "Two", "Blue"),
            ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish"),
            ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish", true)
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(StringV(One Fish Blue Fish), StringV(One Fish Two Fish), StringV(One Fish Two FisT))
[
  ReplaceStrRegex('One Fish Two Fish', 'Two', 'Blue'),
  ReplaceStrRegex('One Fisk Two FisT', 'Fis.', 'Fish'),
  ReplaceStrRegex('One Fisk Two FisT', 'Fis.', 'Fish', true),
]
[ 'One Fish Blue Fish', 'One Fish Two Fish', 'One Fish Two FisT' ]
Query metrics:
  •    bytesIn: 235

  •   bytesOut:  75

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 3ms

  •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!