skij
asked on
JavaScript: Get Message from Data
Using JavaScript, how can I extract the [Message] from this data?
I would like it to match, regardless of the amount of whitespace here:
[Message]=>
[Message] =>
[Message] =>
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
)
)
)
I would like it to match, regardless of the amount of whitespace here:
[Message]=>
[Message] =>
[Message] =>
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);
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] );
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]);
}
Not much simpler than the other solution...
ASKER
I only want the first match. I don't want an array. Only the first [Message] should be returned.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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]);
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
2. By looking for an opening '[' and triming the result
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.
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]);
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());
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.
ASKER
Thanks for pointing that out! Your previous solution works when used with my real data.
Glad to help.
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.