CREATE FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original text)
RETURNS text CHARSET utf8
DETERMINISTIC
BEGIN
DECLARE temp text;
DECLARE temp2 text;
DECLARE ch VARCHAR(1);
DECLARE i INT;
DECLARE j INT;
SET i = 1;
SET temp = '';
set temp2 ='';
set j=1;
IF original REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(original) THEN
LEAVE loop_label;
END IF;
SET ch = SUBSTRING(original,i,1);
SET temp = CONCAT(temp,ch);
IF temp REGEXP pattern THEN
set j=i;
set temp2 ='';
loop_label2: LOOP
IF j<1 THEN
LEAVE loop_label2;
END IF;
set temp2=CONCAT(SUBSTRING(temp,j-1,1),temp2);
IF temp2 REGEXP pattern THEN
set temp = REPLACE(temp,temp2,replacement);
set temp2 ='';
end if;
set j=j-1;
END LOOP;
END IF;
SET i=i+1;
END LOOP;
ELSE
SET temp = original;
END IF;
RETURN temp;
END