I have been developing a script incrementally and it's being executed fine.
expression.function(...) is not a function
.replace() is a text function, so if xyz.replace('a','b') throws the error '. . . is not a function', then xyz is probably not text, but some text object. In that case, you need xyz.contents.replace('a','b').