web stats
Mirth Community - View Single Post - Search for alpha characters and delete OBX
View Single Post
  #4  
Old 05-13-2019, 12:26 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 754
agermano is on a distinguished road
Default

Brad is on the right track. String.prototype.indexOf takes a string parameter, not a regex. I think you want to use String.prototype.search instead.

Your regex matches a string that starts with 0 or more alpha chars. I don't think that's what you want, as search will return index 0 for any string. You can use the i flag to make it case insensitive.

You probably don't want to nest your loops. Wait until you're finished deleting to renumber them instead of renumbering on every delete.

If you are deleting using an index, you need to iterate in reverse and delete from the back first because it will change the index of the segments following the one you delete. I.e. if you have 5 segments and want to delete the second and third, if you delete the second first, everything after it shifts down, and you will end up skipping the 3rd and deleting the 4th instead.

Code:
// delete with a for each
for each (var obr in msg.OBX) {
    if (obx['OBX.5']['OBX.5.1'].toString().search(/[A-Z]/i) !== -1) {
        delete obx[0];
    }
}

// OR delete iterating backwards
for (var i = msg.OBX.length() - 1; i >= 0; i--) {
    if (msg['OBX'][i]['OBX.5']['OBX.5.1'].toString().search(/[A-Z]/i) !== -1) {
        delete msg['OBX'][i];
    }
}

// renumber after all of your deletes are finished
for (var i = 0; i < msg.OBX.length(); i++) {
    msg['OBX'][i]['OBX.1']['OBX.1.1'] = i + 1;
}
Reply With Quote