简体   繁体   English

如何在Mirth中运行javascript正则表达式

[英]How to run javascript Regular Expression in Mirth

I have a the following string: 我有以下字符串:

Source: "HKID:A1234567~PKey:00888880~DOC:TKWC033330"

Regex:  .*(HKID:.*?)(.+?)((?=~)|\s|\z)

When I test this at the JavaScript Regular Expression Test site I got A1234567 so all are good. 当我在JavaScript正则表达式测试站点上对此进行测试时,我得到了A1234567,所以一切都很好。

I put this expression in a javascript transformer in my Mirth channel. 我将此表达式放在我的Mirth频道的javascript转换器中。 But the hk_id value i get back is either null or empty string. 但是我返回的hk_id值为null或空字符串。

Things I've tried: 我尝试过的事情:

  1. use the function re.match() but this gives me error, mirth says cannot find function match in object... 使用函数re.match()但这给了我错误, re.match()cannot find function match in object...
  2. I tried to put single quote around the regular expression below, or the / 我试图在下面的正则表达式或/周围加上单引号
  3. I tried to reduce my regular expression to a simplest form to test which is re.exec('.*') and yet I still get the empty or null value. 我试图将正则表达式简化为最简单的形式来测试re.exec('.*') ,但仍然得到空值或null值。
  4. Instead of running RegExp.$1 , I tried return m only, but no differences were made. 我没有运行RegExp.$1 ,而是尝试仅返回m,但是没有任何区别。

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. 我认为这可能归结为我如何逃避角色,但我找不到有关此的任何Mirth文档,如果您有任何见解,将不胜感激。

var hk_id = Find_HKID();
var xml_msg = 
'<?xml version="1.0" encoding="utf-8" ?> <XML><Barcode="'+hk_id+'" /></XML>';

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 + "";
}

You confused regex and and test string, it should be: 您将正则表达式和测试字符串混淆了,它应该是:

function Find_HKID() 
{
   var test = 'HKID:A1234567~PKey:00888880~DOC:TKWC033330'
   var re = new RegExp('.*(HKID:.*?)(.+?)((?=~)|\s|\z)');
   var m = re.exec(test);
   return RegExp.$1 + RegExp.$2 + RegExp.$3 + "";
}

Now it works like a charm:-D 现在它就像一种魅力:-D

And btw. 顺便说一句。 you should not use new RegExp() , it's slow and ugly. 您不应该使用new RegExp() ,它速度慢且难看。 Use the regex directly: 直接使用正则表达式:

var re = /.*(HKID:.*?)(.+?)((?=~)|\s|\z)/;

Edit: like Ωmega proposed this regex might work for you too, and is much more precise: 编辑:就像Ωmega提出的一样,此正则表达式也可能对您有用,并且更精确:

var re = /.*(HKID:.*?)[~\s]/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM