Link to home
Start Free TrialLog in
Avatar of sidwelle
sidwelleFlag for United States of America

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
var msg = "MSH|...|...|\rGT1|...|...|GT1|...|...|\r";
msg = msg.replace(/\|GT1\|/g, "|\rGT1|");

Open in new window

// This leaves me with:
MSH|...|...|\r
GT1|...|...|\r
GT1|...|...|\r

Open in new window

// 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 ^

Open in new window

I need to avoid adding a double \r\r !

Thank you.
Avatar of zc2
zc2
Flag of United States of America image

Did you try
msg.replace(/[^\r]GT1\|/g, "|\rGT1|")

Open in new window

Avatar of sidwelle

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.
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 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
You are welcome!