JavaScript: Get Message from Data

Using JavaScript, how can I extract the [Message] from this data?
stdClass Object
(
    [HighestSeverity] => ERROR
    [Notifications] => Array
        (
            [0] => stdClass Object
                (
                    [Severity] => ERROR
                    [Source] => ship
                    [Code] => 3058
                    [Message] => Recipient Postal code or routing code is required
                    [LocalizedMessage] => Recipient Postal code or routing code is required
                )
            [1] => stdClass Object
                (
                    [Severity] => ERROR
                    [Source] => ship
                    [Code] => 3431
                    [Message] => International Importer of Record - Postal code or routing code is required
                    [LocalizedMessage] => International Importer of Record - Postal code or routing code is required
                )

        )
)

Open in new window


I would like it to match, regardless of the amount of whitespace here:
[Message]=>
[Message]  =>
[Message]      =>
LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Brian TaoSenior Business Solutions ConsultantCommented:
It looks like you're trying to use javascript to parse a object dumped from PHP.
If so, the correct way is to use PHP's json_encode() function and then you can use javascript's JSON.parse() to get the values.
skijAuthor Commented:
Thanks, but that won't work because the data is actually coming through a compiled script that I don't have access to.  I need to use REGEX for this.
Julian HansenCommented:
Something like this
// ASSUMES DATA IS IN data. SPLIT INTO LINES
var newmsg = data.split("\n");
// ARRAY FOR RESULTS
var results = [];

// LOOP THROUGH LINES
for(var i = 0; i< newmsg.length; i++) {
  // TRY TO MATCH MESSAGE LINE - ADD TO RESULT IF MATCHED
  if (match = newmsg[i].match(/\[Message\] => (.*)/)) {
    // ADD FIRST MATCHED SUBSTRING TO ARRAY
    results.push(match[1]);
  }
}
console.log(results);

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

skijAuthor Commented:
Isn't there a simpler way to do this?  My code  below ALMOST works.  It stops at the next "u" but I want it to stop at the next newline.
var data = 'stdClass Object\
(\
    [HighestSeverity] => ERROR\
    [Notifications] => Array\
        (\
            [0] => stdClass Object\
                (\
                    [Severity] => ERROR\
                    [Source] => ship\
                    [Code] => 3058\
                    [Message] => Recipient Postal code required\
                    [LocalizedMessage] => Recipient Postal code or routing code is required\
                )\
            [1] => stdClass Object\
                (\
                    [Severity] => ERROR\
                    [Source] => ship\
                    [Code] => 3431\
                    [Message] => International Importer of Record Required\
                    [LocalizedMessage] => International Importer of Record - Postal code or routing code is required\
                )\
\
        )\
)';


alert( data.match(  /\[Message\](\s)?=>(.[^u]+)/ )[2]  );

Open in new window

Julian HansenCommented:
You would need to use RegExp to find multiple instances
// The regular expression
var re = /\[Message\]\s*?\=\>\s*(.*)/g;
// Results array
var results = [];
var match;
// Keep matching till all done
while((match = re.exec(data)) !== null) {
   results.push(match[1]);
}

Open in new window

Not much simpler than the other solution...
skijAuthor Commented:
I only want the first match.  I don't want an array.  Only the first  [Message] should be returned.
Julian HansenCommented:
Oh in that case
var re = /\[Message\]\s*?\=\>\s*(.*)/;
alert(data.match(re)[1]);

Open in new window

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
skijAuthor Commented:
Thanks for all your help with this but it still is not working.  Please test this, thanks.
var data = '(\
    [HighestSeverity] => ERROR\
    [Notifications] => Array\
        (\
            [0] => stdClass Object\
                (\
                    [Severity] => ERROR\
                    [Source] => ship\
                    [Code] => 3058\
                    [Message] => Recipient Postal code required\
                    [LocalizedMessage] => Recipient Postal code or routing code is required\
                )\
            [1] => stdClass Object\
                (\
                    [Severity] => ERROR\
                    [Source] => ship\
                    [Code] => 3431\
                    [Message] => International Importer of Record Required\
                    [LocalizedMessage] => International Importer of Record - Postal code or routing code is required\
                )\
\
        )\
)';

var re = /\[Message\]\s*?\=\>\s*(.*)/;
alert(data.match(re)[1]);

Open in new window

Julian HansenCommented:
This all depends how you are going to get your data. Your text data above is effectively one long line - the continuation character '\' cancels out the CR so the regex is matching to the end of the data
So we need to break the search. There are many ways we can do that.

1. By looking for more than one space next to each other
var re = /\[Message\]\s*?\=\>\s*(.*?)\s\s+/;
var match = data.match(re);
console.log(match[1]);

Open in new window


2. By looking for an opening '[' and triming the result
var re = /\[Message\]\s*?\=\>\s*(.[^\[]*)/;
var match = data.match(re);
console.log(match[1].trim());

Open in new window


However you need to consider where your data is ultimately going to be coming from - if it is from a source where there will be '\n' delimited lines then the original solution would apply - the above would not work.
skijAuthor Commented:
Thanks for pointing that out!  Your previous solution works when used with my real data.
Julian HansenCommented:
Glad to help.
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
JavaScript

From novice to tech pro — start learning today.