Announcement

Collapse

Mirth Connect 3.12.0 Released!

Mirth Connect 3.12.0 is now available as an appliance update and on our GitHub page. This release includes database performance improvements, improves visual HL7 representation, message pruning, keystore handling, PDF generation, community contributions, and fixes several security vulnerabilities. This release also contains many improvements to commercial extensions. See the release notes for the list of fixes and updates.

Download | See What's New | Upgrade Guide | Release Notes

For discussion on this release, see this thread.
See more
See less

odd javascript regexp replace behavior

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • odd javascript regexp replace behavior

    Though I can work around what I see I am puzzled why, when I want to esc sql unfriendly chars like single quotes and backslashes I hit odd behavior in the javascript with a Mirth transformer. To esc single quotes using Perl replacing each single quote by two single quotes I can do it as:
    $foo =~ s/'/''/g;

    Something close to that in Mirth gave error messages so Plan B was:

    Code:
    // function to esc chars for sql
    function esc_sql(origstr)
    {
       if ((origstr != null) && (origstr != ""«»))
       {
    	if (origstr.indexOf("'"«») >= 0)
    		{
    		origstr = origstr.replace(/'/g,"##squote##"«»);
    		origstr = origstr.replace(/"##squote##"/g,"''"«»);
    		}
    	if (origstr.indexOf("\"«») >= 0)
    		{
    		origstr = origstr.replace(/\\/g,"##bslash##"«»);
    		origstr = origstr.replace(/"##bslash##"/g,"\\\"«»);
    		}
        }
        return origstr;
    }
    the above worked in simple tests with short strings but gave errors in others for the replace to insert the double single quotes. What did work consistently was:

    Code:
    // function to esc chars for sql
    function esc_sql(origstr)
    {
       if (origstr != null)
       {
    	//Escape illegal characters
    	var index = 0;
    	var StrOut = "";
    	//asingle quotes
    	for (index = origstr.indexOf("'"«»); index != -1; index = origstr.indexOf("'"«»))
    	  {
    	  // Copy up to the apostrophe
    	  StrOut += origstr.substring(0, index);
    	  // Add double apostrophe
    	  StrOut += "''";
    	  origstr = origstr.substring(index + 1);
    	  //Chop off "used" part
    	  }
    	  StrOut += origstr; 
    	  origstr = StrOut;
    	  channelMap.put('origstr', origstr);
    	// now deal with backslash
    	var index = 0;
    	var StrOut = "";
    	//asingle quotes
    	for (index = origstr.indexOf("\"«»); index != -1; index = origstr.indexOf("\"«»))
    	  {
    	  // Copy up to the backslash
    	  StrOut += origstr.substring(0, index);
    	  // Add double backslash
    	  StrOut += "\\\";
    	  origstr = origstr.substring(index + 1);
    	  //Chop off "used" part
    	  }
    	  StrOut += origstr; 
    	  origstr = StrOut;
        }
        return origstr;
    }
    It would be interesting for me to understand what causes this to happen and if there is a more elegant way around such problems. Thanks.
Working...
X