问题描述:
有个字符串,格式如下:
var vcard='<vCard xmlns="vcard-temp"><N><GIVEN>1</GIVEN></N><BDAY>2</BDAY><EMAIL>3</EMAIL><URL>4</URL><ADR><CTRY>5</CTRY><REGION>6</REGION><LOCALITY>7</LOCALITY><STREET>8</STREET><EXTADD>9</EXTADD><PCODE>10</PCODE></ADR><ORG><ORGNAME>11</ORGNAME><ORGUNIT>xxxxxxxxxxxxx</ORGUNIT></ORG><TITLE>12</TITLE><ROLE>13</ROLE><DESC>14</DESC></vCard>';
想通过js获取组织信息
var ORGUNIT =getUnit(vcard);
function getUnit(vcard){
//[TODO] get unit from vcard...
return "unit";
}
这个getUnit方法该如何实现?
javascript
function
------解决方案--------------------
var vcard='<vCard xmlns="vcard-temp"><N><GIVEN>1</GIVEN></N><BDAY>2</BDAY><EMAIL>3</EMAIL><URL>4</URL><ADR><CTRY>5</CTRY><REGION>6</REGION><LOCALITY>7</LOCALITY><STREET>8</STREET><EXTADD>9</EXTADD><PCODE>10</PCODE></ADR><ORG><ORGNAME>11</ORGNAME><ORGUNIT>xxxxxxxxxxxxx</ORGUNIT></ORG><TITLE>12</TITLE><ROLE>13</ROLE><DESC>14</DESC></vCard>';
var t,reg = /<ORGUNIT>([\s\S]*?)<\/ORGUNIT>/ig;
while((t=reg.exec(vcard))!=null){
alert(t[1]);
}
------解决方案--------------------
split(">")方法进行参数为循环所得到数组当值为"<ORGUNIT"那么下一个就是xxxxxxxxxxxxx</ORGUNIT 然后再用split("<")得到数组的第一个就是了