Note ## removes longest prefix match, # removes shortest. %% removes longest suffix, % removes shortest. These are pure Bash operations (no subprocess), so they are much faster than calling sed or basename in a loop.
// Backreference in replacement'John Smith'.replace(/(\w+) (\w+)/, '$2, $1')
// Function replacement'hello'.replace(/\w/g, c => c.toUpperCase())
output
"Smith, John"
"HELLO"
Note In the replacement string, $1 $2 refer to captured groups, $& is the full match, $` is before-match, $' is after-match. Use a function for dynamic replacements. replaceAll() requires the g flag on regex arguments.