The documentation is already 100% perfectly clear what
Replacement is: it is a string.
You seem to think that it RegExReplace takes some special and magical argument, but no, it is nothing more than a string.
The catch here is that RegExReplace also
interprets Replacement and replaces any $number, ${number}, ${capturename} etc. in Replacement before replacing the found Needle in Haystack with the munged Replacement.
mikeyww wrote: ↑23 Jan 2023, 11:12
Code: Select all
#Requires AutoHotkey v2.0
str := "abcd"
regex := "ab(c)d"
f := "123$1q9"
MsgBox RegExReplace(str, regex, 'J' f "$1q9") ; A backreference
MsgBox RegExReplace(str, regex, 'J' InStr(f, "$1q9")) ; Not a backreference
MsgBox RegExReplace(str, regex, 'J' SubStr("$1", 1, 1)) ; Not a backreference
MsgBox RegExReplace(str, regex, 3 + "$1") ; Error
Consider this:
Code: Select all
str := "abcd"
regex := "ab(c)d"
f := "123$1q9"
MsgBox 'J' f "$1q9" ; A backreference
MsgBox 'J' InStr(f, "$1q9") ; Not a backreference
MsgBox 'J' SubStr("$1", 1, 1) ; Not a backreference
MsgBox 3 + "$1" ; Error
The first MsgBox will show
J123$1q9$1q9
The second MsgBox will show
J4
The third MsgBox will show
J$
The fourth MsgBox will throw an error:
Expected a Number but got a String
Therefore, the code at the top behaves identically to the following:
Code: Select all
MsgBox RegExReplace("abcd", "ab(c)d", "J123$1q9$1q9")
MsgBox RegExReplace("abcd", "ab(c)d", "J4")
MsgBox RegExReplace("abcd", "ab(c)d", "J$")
MsgBox RegExReplace("abcd", "ab(c)d", 3 + "$1")
Notice how there are no "function calls" "inside" Replacement or any "variables". That's because there is no magical behavior in Replacement. Replacement will be the result of whatever expression is in there, exactly as with every single other function call in AHK 2.0.
In the first RegExReplace, every $1 in Replacement was replaced with "c" because the first capture captured the "c". Then, the entire input string (because the Needle matches all of it) is replaced with the processed Replacement string, which is now "J123cq9cq9".
In the second RegExReplace, the entire string is replaced with "J4". That's because
InStr(f, "$1q9") evaluates to the number 4 and "J" concatenated with 4 is "J4". There is no backreference there because there is no backreference there. There is no $ followed by a number or a { here, so nothing is changed in Replacement.
The same happens in the third RegExReplace because the $ at the end of the string by itself is not a backreference, either.
The fourth one throws an error not because of any RegExReplace behavior, but because there is no possible way AutoHotkey could determine what you meant by
3 + "$1". What do you think this should result in? It's adding apples and oranges. In JavaScript, perhaps you could get a fruit salad because it specifies that adding two things together with + results in the concatenation of the things after converting them both to strings. But in AutoHotkey, + adds together two numbers and "$1" cannot be cleanly interpreted as a number, so it tells you that it expected a number on the other side of that plus sign.
Now look at this:
Code: Select all
str := "abcd"
regex := "(a)(b)(c)(d)"
f := "123$1q9"
MsgBox RegExReplace(str, regex, 'J$' InStr(f, "$1q9"))
This will output "Jd". This is because Replacement is "J$4" because (d) is the fourth capture and
InStr(f, "$1q9") evaluates to 4.
I do agree that the documentation for Replacement should assume less about what the reader knows and introduce the backreferences a bit more cleanly and gently.
All in all, you should have said what you actually expected each of these lines of code to do.