Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

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]      =>
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

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.
Avatar of skij

ASKER

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

Avatar of skij

ASKER

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

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...
Avatar of skij

ASKER

I only want the first match.  I don't want an array.  Only the first  [Message] should be returned.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa 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
Avatar of skij

ASKER

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

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.
Avatar of skij

ASKER

Thanks for pointing that out!  Your previous solution works when used with my real data.
Glad to help.