I am using jQuery jTable plugin to display a session variable data stored in my mysql db which will be in the format:
gateway|i:1;FirstName|s:7:"Shelley";LastName|s:2:"VS";Status|s:7:"Enabled";UID|s:10:"b81ef6a12a";AdminGroup|s:8:"Sysadmin";
I am displaying all the session data in a jtable column which is not required. I need to extract only the FirstName portion from the string using JavaScript. I am a javascript newbie. I did the following:
name: {
title: 'Admin Name',
display: function (data) {
var x = data.record.data;
if(x.match("FirstName")) {
return "yes";
}
},
},
Which displays "yes" in my new jtable column as FirstName data is found in string using x.match. How can I extract only the first name portion from the above text using javascript. I will have multiple records in the same format.
Thanks in advance.
Added to my original post
@cytofu Excellent answer to a newbie..I changed the code to suit my jTable (without an additional function) as follows and it is running fine: Thank you.
firstname: {
title: 'First Name',
display: function (data) {
var str = data.record.data;
var startOfSection = str.indexOf('FirstName');
var startOfValue = str.indexOf('"',startOfSection)+1;
var endOffValue = str.indexOf('"',startOfValue); //one char longer, as needed for substring
var value = str.substring(startOfValue,endOffValue);
return value;
},
},
//last name
lastname: {
title: 'Last Name',
display: function (data) {
var str = data.record.data;
var startOfSection = str.indexOf('LastName');
var startOfValue = str.indexOf('"',startOfSection)+1;
var endOffValue = str.indexOf('"',startOfValue); //one char longer, as needed for substring
var value = str.substring(startOfValue,endOffValue);
return value;
},
},