-1

我有一个字符串中的数据,我需要进一步过滤数据,将其保存在另一个字符串变量中。使用 jQuery。

例如,我有

 var str= 'a="apple",a="ajax",b="boy",b="bird",b="ball".........';

rom 这个我想要 a="" 的数据,所以需要保存苹果,ajax

以下数据以字符串格式从 Web 服务返回:

<?xml version="1.0" encoding="utf-16"?><TermStore><T a9="5af46de3-630b-4cd9-aff3-4d1fbb95333c" a21="false" a61="0"><LS>< TL a32="SharePoint" a31="true" /><TL a32="MOSS" a31="false" /><TL a32="SharePoint 2007" a31="false" /><TL a32="SharePoint 2010" a31="false" /><TL a32="SharePoint 2013" a31="false" /><TL a32="SharePoint 管理员" a31="false" /><TL a32="SharePoint Designer" a31="false" /><TL a32="SharePoint Developer" a31="false" /><TL a32="SharePoint Infopath" a31="false" /><TL a32="WSS" a31="false" /></LS><DS><TD a11="这是一个基于 Web 的可扩展平台" /></DS><TMS><TM a24="f03e532e-6e71-48c4-b499-c93efc7a4f7b" a12="Microsoft" a40 ="" a17="true" /></TMS></T></TermStore>`

由此我只需要过滤a32="", 值并将其存储在不同的字符串中。

4

3 回答 3

0

在我的脑海中,这应该可以完成工作

var get='a';
var split = str.split(",");
var final = '';
for(var i in split)
{
    split2 = split[i].split("=");
    if(split2[0] == get)
        final += split[i]+','
}
var str2 = final.substring(0,final.length-1);
于 2012-10-11T14:45:00.357 回答
0

看jquery parsexml http://api.jquery.com/jQuery.parseXML/

根据其文档,它应该是

var xml = '<?xml version="1.0" encoding="utf-16"?><TermStore><T a9="5af46de3-630b-4cd9-aff3-4d1fbb95333c" a21="false" a61="0"><LS><TL a32="SharePoint" a31="true" /><TL a32="MOSS" a31="false" /><TL a32="SharePoint 2007" a31="false" /><TL a32="SharePoint 2010" a31="false" /><TL a32="SharePoint 2013" a31="false" /><TL a32="SharePoint Admin" a31="false" /><TL a32="SharePoint Designer" a31="false" /><TL a32="SharePoint Developer" a31="false" /><TL a32="SharePoint Infopath" a31="false" /><TL a32="WSS" a31="false" /></LS><DS><TD a11="It is an extensible web based Platfrom" /></DS><TMS><TM a24="f03e532e-6e71-48c4-b499-c93efc7a4f7b" a12="Microsoft" a40="" a17="true" /></TMS></T></TermStore>';
xmlDoc = $.parseXML( xml );
$xml = $( xmlDoc );
var a32="";
$title = $xml.find( "[a32]" ).each(function(){
a+=$(this).attr('a32');
});
于 2012-10-12T06:25:26.440 回答
0
var string = 'a="apple",a="ajax",b="boy",b="bird",b="ball"';

function filterIt(str){
   var filtered = [];
   str.replace(/a="(.*?)"/g, function(a, b){
       filtered.push(b);
   });
   return filtered.join(",");
}

filterIt(string);
于 2012-10-11T14:59:27.910 回答