前端编辑
正则表达式匹配字元串
原字元串responseStr:
<document>
<response id="response">
<head>
<a>1</a>
<b>ABC</b>
<c>add.fgfg.dfdf</c>
<d>20181115114315</d>
<e>7868sdfsdf9789adelkj</e>
</head>
<body>
<f>2018-11-15 11:43:15</f>
</body>
</response>
</document>
匹配:
var aResult= responseStr.match(/<a>(S*)</a>/);
aResult="<a>1</a>,1"
var fResult= responseStr.match(/<f>(S*)</f>/);
fResult=null
var fResult= responseStr.match(/<f>(d{4}-d{2}-d{2}sd{2}:d{2}:d{2})</f>/);
fResult="<f>2018-11-15 11:43:15</f>,2018-11-15 11:43:15"
var fResult= responseStr.match(/<f>(d{4}-d{2}-d{2}sd{2}:d{2}:d{2})</f>/g);
fResult="<f>2018-11-15 11:43:15</f>"//g是正则中的全局匹配,即匹配到第一个之后就停止,所以只会获取第一次查询结果,无法获取f中间的值。
推荐阅读: