我有一个字符串中的数据,我需要进一步过滤数据,将其保存在另一个字符串变量中。使用jQuery。
例如,我有
var str= 'a="apple",a="ajax",b="boy",b="bird",b="ball".........';
这个我想要数据a =“”,所以苹果,ajaxneed要保存
以下数据以字符串格式从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 =“它是可扩展的 基于网络的平台“/>< / DS>< TMS>< TM a24 =“f03e532e-6e71-48c4-b499-c93efc7a4f7b”a12 =“Microsoft”a40 =“” A17 = “真” />< / TMS>< / T>< / TermStore>`
从此我需要仅过滤a32=""
,值并将其存储在不同的字符串中。
答案 0 :(得分: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);
答案 1 :(得分: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);
答案 2 :(得分: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');
});