shels
shels

Reputation: 21

Javascript extract value from string

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;
},
},

Upvotes: 0

Views: 6009

Answers (2)

cytofu
cytofu

Reputation: 903

add this function to the script:

function extractValue(str,searchStr){

    var startOfSection = str.indexOf(searchStr);
    var startOfValue = str.indexOf('"',startOfSection)+1;
    var endOffValue  = str.indexOf('"',startOfValue); //Position of first char AFTER the value, as needed for substring

    var value = str.substring(startOfValue,endOffValue);

    return value;
}

you can test it with:

var str = 'gateway|i:1;FirstName|s:7:"Shelley";LastName|s:2:"VS";Status|s:7:"Enabled";UID|s:10:"b81ef6a12a";AdminGroup|s:8:"Sysadmin";';
var searchStr = 'FirstName|';

alert (extractValue(str,searchStr));

change your jTable function fragment as follows:

name: {
    title: 'Admin Name',
    display: function (data) {
        var x = data.record.data;
        if(x.match("FirstName")) {
            return extractValue(x,"FirstName");
        }
    },
},

fiddle: http://jsfiddle.net/nBG89/7/

Upvotes: 0

Tommi
Tommi

Reputation: 3247

You have three common ways:

  • parse whole string to object. .split() is your best friend in this case;
  • change incoming format to JSON;
  • get your data with regex:

    var patient = 'gateway|i:1;FirstName|s:7:"Shelley";LastName|s:2:"VS";Status|s:7:"Enabled";UID|s:10:"b81ef6a12a";AdminGroup|s:8:"Sysadmin";';

    alert(patient.match(/FirstName\|\w\:\d+\:\"(\w+)\"/)[1]);

Upvotes: 2

Related Questions