当前位置:编程学习 > JS >>

js截取字符串

看来正则需要好好学习学习了,今天遇到了个截取字符串的问题。
有个字符串,格式如下:
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>';
所需<ORGUNIT>xxxxxxxxxxxxx</ORGUNIT>中的值
 
解决方法如下:
[javascript]  
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]);  
    }  
 
dml@2013.1.7
补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,