sidwelle
asked on
JavaScript, Regular-expression, Replace
JavaScript, Regular-expression, Replace
I am trying to use a regular expression to correct an HL7 msg.
Using 'replace' to search the inbound text message and find segments that are not correctly terminated w/carriage returns.
Example: // this works
How do I search for any character in the 1st (search) argument and get it into the 2nd (replace) argument ?
Thank you.
I am trying to use a regular expression to correct an HL7 msg.
Using 'replace' to search the inbound text message and find segments that are not correctly terminated w/carriage returns.
Example: // this works
var msg = "MSH|...|...|\rGT1|...|...|GT1|...|...|\r";
msg = msg.replace(/\|GT1\|/g, "|\rGT1|");
// This leaves me with:MSH|...|...|\r
GT1|...|...|\r
GT1|...|...|\r
// but, I don't always know that the 2nd instance of 'GT1' will be proceeded w/a '|' (pipe) character.How do I search for any character in the 1st (search) argument and get it into the 2nd (replace) argument ?
var msg = "MSH|...|...|\rGT1|...|...|GT1|...|...|\r";
msg = msg.replace(/.GT1\|/g, ".\rGT1|");
// What goes here ^ and Here ^
I need to avoid adding a double \r\r !Thank you.
ASKER
No, wouldn't that replace any line that begins with "\rGT1|" w/ "|\rGT1|" ?
I don't know what 'GT1' will be prefixed with, I just don't it be acted on if is prefixed with \r.
I need strings of '\rGT1|' to be excluded from the replace.
Thank you.
I don't know what 'GT1' will be prefixed with, I just don't it be acted on if is prefixed with \r.
I need strings of '\rGT1|' to be excluded from the replace.
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That line works, not sure what '$1' does. I suppose it replaces the first token, can't seem to find any documentation on it ?
Thanks
Thanks
ASKER
Thanks for the help, that's what I needed to see !
Sorry I can't provide a more complex example, all the msgs are full of PHI.
Thanks
Sorry I can't provide a more complex example, all the msgs are full of PHI.
Thanks
You are welcome!
Open in new window