Postgresql reverse string with backslash?

Greetings. I'm trying to write a function to remove the \path\to\a\file.jpg so all i'm left with is the file.jpg

My strategy is to reverse the test, look for the first backslash, then kill that backslash and everything after it. then reverse again.

But my new function fails. When i step through it with a debugger it SEEMS to work but then when i run it no go.

what am i doing wrong?!! I've broken the function down to simple components to try and debug it. I'm not even doing the path-stripping part yet since i can't get the reverse to work with the backslashes....