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.
sidwelleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

0
sidwelleAuthor Commented:
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.
0
zc2Commented:
Could you give more samples of possible input lines and desired results?

Let's say
var msg = "MSH|...|...|\rGT1|...|...|GT1|...|@GT1|...|\r";

Open in new window

Then this expression
msg.replace(/([^\r])GT1\|/g, "$1\rGT1|")

Open in new window

creates
"MSH|...|...|\rGT1|...|...|\rGT1|...|@\rGT1|...|\r"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sidwelleAuthor Commented:
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
0
sidwelleAuthor Commented:
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
0
zc2Commented:
You are welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.