title: How to run javascript Regular Expression in Mirth
body: <p>I have a the following string: </p>
  
  <pre><code>Source: "HKID:A1234567~PKey:00888880~DOC:TKWC033330"
  
  Regex:  .*(HKID:.*?)(.+?)((?=~)|\s|\z)
  </code></pre>
  
  <p>When I test this at the JavaScript Regular Expression Test <a href="http://www.regular-expressions.info/javascriptexample.html" rel="nofollow">site</a> I got A1234567 so all are good.</p>
  
  <p>I put this expression in a javascript transformer in my Mirth channel.  But the hk_id value i get back is either null or empty string.  </p>
  
  <p>Things I've tried:</p>
  
  <ol>
  <li>use the function <code>re.match()</code> but this gives me error, mirth says
  <code>cannot find function match in object...</code></li>
  <li>I tried to put single quote around the regular expression below, or the <code>/</code> </li>
  <li>I tried to reduce my regular expression to a simplest form to test which is <code>re.exec('.*')</code> and yet I still get the empty or null value.</li>
  <li>Instead of running <code>RegExp.$1</code>, I tried return m only, but no differences were made.</li>
  </ol>
  
  <p>I think it may boil down to how I escape the characters, but I can't find any Mirth document about this, if you have any insight they will be greatly appreciated.</p>
  
  <pre><code>var hk_id = Find_HKID();
  var xml_msg = 
  '&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;XML&gt;&lt;Barcode="'+hk_id+'" /&gt;&lt;/XML&gt;';
  
  var sResp = ResponseFactory.getSuccessResponse(xml_msg)
  responseMap.put('Response', sResp);
  
  
  function Find_HKID() 
  {
     var test = 'HKID:A1234567~PKey:00888880~DOC:TKWC033330'
     var re = new RegExp(test);
     var m = re.exec('.*(HKID:.*?)(.+?)((?=~)|\s|\z)');
     return RegExp.$1 + RegExp.$2 + RegExp.$3 + "";
  }
  </code></pre>