Or is it the \
characters, also special in KM text fields -- which IMO is why the search is failing, so $1
isn't populated...
There are just too many possible problems trying use special characters in a prompt to fill a variable to use in a regex. I think @ComplexPoint point's approach is better -- if you want a user-defined regex search and replace, hand it off.
Since using AppleScript means we can leverage KME's regex engine, with all the goodies we know and love (and nothing to do with me knowing zero JavaScript!) -- here's my go at it:
Find and Replace in current segment - AS.kmmacros (4.4 KB)
Works with OP's text/requirements, but I haven't tried many other regexs -- so test thoroughly!