Using to_base64 function and regexp_replace with backreferences in MariaDB
The algorythm as I see it.

We don't know how many replaces we need. So we must use recursive CTE or iterative/recursive code.

In it we must:


Find first pattern occurency offset in source data starting from some offset (initially 1)
Split source by head, pattern and tail
Perform pattern replace and encode
Concat head and encoded replace, calculate its length (it will be used as search starting offset on the next iteration)
Concat tail
Iterate next if current starting offset (last "first pattern occurency offset" - see step 1) > 0


In main query we select only those records where "first pattern occurency offset" = 0.

This algo can be realized:


using local variables @variable performing all chained calculations in excess fields which will be skipped in main query
in user-defined function which processes one literal value
in stored procedure


The process can be simplified if there is a guarantee that only one pattern match exists - but in that case the result for values which contains more occurences (randomly) will be unpredictable.