Javascript Regex to get specific string from two differently-formatted text blocks

143 views Asked by At

I need a single regex that I can use in Javascript that would allow me to extract just the tracking number from two different formats of plain text order confirmation emails:

First confirmation email:

(the tracking number is always the line right below "Tracking Number")

Tracking Number
1ZA828Y12313205351

Second confirmation email:

(the tracking number is always on the same line, after the colon and a (possibly) variable number of space characters)

Carrier Tracking Number :  582612345988

Also, just to clarify: The tracking numbers will always be any combination of capital letters and numbers (and will not necessarily start with "1Z").

2

There are 2 answers

3
vks On BEST ANSWER
(?:Carrier )?Tracking Number\s*:?\s*([A-Z0-9]+)

Try this.Just grab the capture or group.See demo.

https://regex101.com/r/nS2lT4/28

6
user35443 On

What about a simple

[A-Z0-9]([A-Z0-9])+

It may sound ugly, but if you know that there are never two upper-case characters one after another, it will work.