Title says it all...
$bar = xdmp:unquote( fn:replace( xdmp:quote($foo), "string to be replaced", "new string value" ))