mirth

How do you replace \T\ in a Mirth Transformer script?


An HL7 message is encoded using a few special characters |^&~ However sometimes test names in OBR.4 contain the & character and per HL7 standards, it should be encoded as \T\ and replaced with & by the parser. Unfortunately \T represents a tab character in RegEx and I am unable to formulate an expression that can transform Vitamin B12 \T\ Folate to Vitamin B12 & Folate.

Tried to use

var value = msg['OBR']['OBR.4'][OBR.4.2]; // returns "Vitamin B12 \T\ Folate"
value = value.replace(/\\T\\/g, value)

But the above throws the exception TypeError: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified..

Does the replace RegEx expression need to be formulated in a different way for this to work in Mirth?


Solution

  • The trick is to ensure the string is a JavaScript string and not a Java string. Here is a function I ended up creating that works

    function _replaceHL7EncodedCharacters(value) {
            value = value.toString(); // convert to Javascript string
            value = value.replace(/\\T\\/g, '&');
            value = value.replace(/\\R\\/g, '~');
            value = value.replace(/\\E\\/g, '\\');
            value = value.replace(/\\F\\/g, '|');
            value = value.replace(/\\S\\/g, '^');
    
            return value;
        }