-1

I want to understand the below XPath expression

fn:replace ($var,concat('^.*',fn:replace('.','(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')),'')

I understand
\ denotes an escape character
| denotes OR
. denotes any single character

But I am confused with the use of \$1 here. What is it referring to?

Prophet
  • 32,350
  • 22
  • 54
  • 79
AverageIQ
  • 7
  • 1

1 Answers1

0

$1 is a back-reference.

The goal of the expression is to replace any regex meta-character (., [, ], and so on) with an escaped version of that character.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • To clarify, that's what the inner fn:replace() call does. The output of the inner fn:replace should then be a regular expression, which is used as the second argument of the outer fn:replace() call. The whole thing could probably have been achieved more easily by using the "q" regex flag - though I think that needs XPath 3.0. – Michael Kay Sep 15 '22 at 10:10