Athapali
Athapali

Reputation: 1089

extract string from inside an attribute

I need to write a function like this:

var width=getWidth( $(this).attr("ows_MetaInfo"));

where $this is pointing to the xml row .

in the getWidth(meta){} function I need to find the text vti_lastheight and do some split and stuff and grab the numeric height value which is 250. How do i do this in javascript or jquery??

<z:row ows_Title='We are together!' ows_ImageSize='620' 
            ows_MetaInfo='16;#vti_parserversion:SR|14.0.0.6120&#13;&#10;                
            vti_lastheight:IW|250&#13;&#10;vti_lastwidth:IW|620&#13;&#10;
            vti_description:SW|Lorem ipsum dolor sit volutpat.' />

Update: I found that someone had written a function to grab the description value from vti_description. Can someone explain to me how this function is able to extract the description portio of text "lorem ipsum' part and how I can make use of it to extract the lastheight value? Please?

function getDescription(metaInfo)
    {
        var description="";
        if(metaInfo!=null)
        {
            metaParts=metaInfo.split("\n");
            if(metaParts!=null && metaParts.length>0)
            {
                var i=0;
                do
                {
                    if(metaParts[i].indexOf("vti_description")>-1)
                    {
                        var descParts=metaParts[i].split("|");
                        if(descParts!=null && descParts.length>1)
                        {
                            description=descParts[1];
                        }
                    }
                    i++;
                } while (i<metaParts.length && description=="" );
            }
        }
        return description;
    }

Upvotes: 3

Views: 153

Answers (2)

xpy
xpy

Reputation: 5621

I believe you should use a regular expression.

function getWidth(str) {
    str = str.replace(/(\r\n|\n|\r)/gm, "");
    str = str.match(/vti_lastwidth:(.*?(?=vti))/);
    str = parseInt(str[1].split('|')[1]);
    console.log(str);
    return str;;
}

Supposing your data will always have this formation.

DEMO:http://jsfiddle.net/pavloschris/3gdd5/

With a minor tweak you could use this function for any attribute.

Upvotes: 1

Jamie Hutber
Jamie Hutber

Reputation: 28076

getWidth is a function. You don't need a function for this simply:

 var width=$(this).attr("ows_MetaInfo");

ows_ImageSize is an attr of this xml row. So calling above should work and it will return whatever is set in ows_ImageSize in this case that is 620. So i would test it first by making sure this is returning a number:

console.info(width);

If it is then you'll know that part works and you can tackle getting getWidth(meta){} to work.

 getWidth(width);

Upvotes: 0

Related Questions