正则表达式 求助,望高手解答
发布网友
发布时间:2022-04-30 23:13
我来回答
共4个回答
热心网友
时间:2022-04-25 13:07
如果你是采集来的代码,就用我的办法,如果是自己的javascript,按cs903016兄说的直接访问就行了。
<?php
header('Content-Type:text/html;charset=utf-8');
$strJson = '[{"SkuId":1153865516,"Size":"均码(胸围84-108)","Color":"粉红色"},
{"SkuId":1153865517,"Size":"均码(胸围84-108)","Color":"浅*"},
{"SkuId":1153865518,"Size":"均码(胸围84-108)","Color":"天蓝色"},
{"SkuId":1153865519,"Size":"均码(胸围84-108)","Color":"水绿色"},
{"SkuId":1153865520,"Size":"均码(胸围84-108)","Color":"桃红色"}]';
$objJson = json_decode($strJson);
foreach ($objJson as $json) {
echo "SkuId: ".$json->SkuId."<br />";
echo "Size: ".$json->Size."<br />";
echo "Color: ".$json->Color."<br />";
echo "<hr />";
}
?>
结果:
思路:先用正则匹配出json字符串,然后将其转换为json对象(如PHP中用json_decode函数),然后通过json对象访问所有数据。
匹配出json字符串的正则:ColorSize = (.*);
热心网友
时间:2022-04-25 14:25
施主,使用的是json数据你非要用正则去匹配,你是认为正则比json的读取方式更加方便么
var ColorSize = [
{"SkuId":1153865516,"Size":"均码(胸围84-108)","Color":"粉红色"},
{"SkuId":1153865517,"Size":"均码(胸围84-108)","Color":"浅*"},
];
这样的一段代码如果你要读取里面的东西,比如你要读取第一个的SkuId
ColorSize[0].SkuId
热心网友
时间:2022-04-25 15:59
<script type="text/javascript">
function pageOnload(){
var html;
var ColorSize = [{"SkuId":1153865516,"Size":"均码(胸围84-108)","Color":"粉红色"},{"SkuId":1153865517,"Size":"均码(胸围84-108)","Color":"浅*"},{"SkuId":1153865518,"Size":"均码(胸围84-108)","Color":"天蓝色"},{"SkuId":1153865519,"Size":"均码(胸围84-108)","Color":"水绿色"},{"SkuId":1153865520,"Size":"均码(胸围84-108)","Color":"桃红色"}];
for(var i=1;i<ColorSize.length;i++){
var SkuId = ColorSize[i].SkuId;
var Size = ColorSize[i].Size;
var Color = ColorSize[i].Color;
var tdHtml = "<tr><td>" + SkuId + "</td>" + "<td>" + Size + "</td>"
+ "<td>" + Color + "</td></tr>";
html += tdHtml;
}
$("#fire").append(html);
}
</script>
<body onload="pageOnload();">
<table id="fire" border="1">
<tr>
<td width="220px">电话号码</td>
<td width="220px">胸围</td>
<td width="220px">胸罩颜色</td>
</tr>
</table>
<div style="background-color: red; height: 50px;">
<center style="margin:0 auto;font-size:24px;">分割线</center>
</div>
热心网友
时间:2022-04-25 17:51
{"SkuId":(.*?),"Size":"(.*?)","Color":"(.*?)"} 试试