fn:replace function

The fn:replace function compares each set of characters within a string to a given pattern. fn:replace replaces the characters that match the pattern with another set of characters.

Syntax

Read syntax diagramSkip visual syntax diagramfn:replace( source-string, pattern, replacement-string, flags)
source-string
A string that contains characters that are to be replaced.

source-string is a literal string, or an Xpath expression that resolves to an xs:string value or the empty sequence.

pattern
A regular expression that is compared to source-string. A regular expression is a set of characters, pattern-matching characters, and operators that define a string or group of strings in a search pattern.

pattern is string literal.

replacement-string
A string that contains characters that replace characters that match pattern in source-string.

replacement-string is an xs:string value. A literal $ symbol must be written as \$. A literal \ symbol must be written as \\.

flags
A string literal that can contain any of the following values that control matching of pattern to source-string:
s
Indicates that the dot (.) matches any character.

If the s flag is not specified, the dot (.) matches any character except the new line character (#x0A).

m
Indicates that the caret (^) matches the start of any line (the position after a new line character), and the dollar sign ($) matches the end of any line (the position before a new line character).

If the m flag is not specified, the caret (^) matches the start of the entire string, and the dollar sign ($) matches the end of the entire string.

i
Indicates that matching is case-insensitive for the letters "a" through "z" and "A" through "Z".

If the i flag is not specified, case-sensitive matching is done.

x
Indicates that whitespace characters (#x09, #x0A, #x0D, and #x20) within pattern are ignored, unless they are within a character class. Whitespace characters in a character class are never ignored.

If the x flag is not specified, whitespace characters are used for matching.

Returned value

If source-string is not the empty sequence, the returned value is an xs:string value that results when the following operations are performed on a copy of source-string:
  • source-string is searched for characters that match pattern.
    • If two overlapping substrings of source-string match pattern, only the substring whose first character comes first in source-string is considered to match pattern.
    • If pattern contains two or more alternative sets of characters, and the alternative sets of characters match characters that start at the same position in source-string, the first set of characters in pattern that matches characters in source-string is considered to match pattern.
  • Each set of characters in source-string that matches pattern is replaced with replacement-string.

If pattern is not found in source-string, source-string is returned.

If pattern matches a string of length zero, an error is returned.

If source-string is the empty sequence, a string of length 0 is returned.

Example

The following function replaces all instances of "a" followed by any single character or "b" followed by any single character with "*@".
fn:replace("abbcacadbdcd","(a(.))|(b(.))","*@")

The returned value is "*@*@*@*@*@cd".