Use Variables as Regex Strings in Search and Replace

The discussion in Rename Files Mystery shows a prompt for both search and replacement strings for a filename that is subsequently executed by either Perl or JavaScript code. Which you might find illuminating.

You might also find interesting Textcavator 2021, which optionally takes a regex search and performs it on a set of files.