87,899
社区成员
发帖
与我相关
我的任务
分享
var s="<root><Property Name='长' Value='11' /><Property Name='宽' Value='22' /><Property Name='属性三' ID='9ee7e7bc-1cdc-47c3-b856-09873c7190b4' Value='E8E' /></root>"
//递归接收一个字符串,返回属性和值
//#region
function MyRecursion(value, result) {
var connectStr = result;
var indexPropertyNameStart = value.indexOf("<Property Name='");
//是否还有属性
if (indexPropertyNameStart != -1) {
value = value.substring(indexPropertyNameStart + 16, value.length);
var indexPropertyNameEnd = value.indexOf("'");
var propertyName = value.substring(0, indexPropertyNameEnd);
connectStr += propertyName + "=";
value = value.substring(indexPropertyNameEnd + 1, value.length);
var indexValueStart = value.indexOf("Value='");
value = value.substring(indexValueStart + 7, value.length);
var indexPropertyValueEnd = value.indexOf("'");
var propertyValue = value.substring(0, indexPropertyValueEnd);
value = value.substring(indexPropertyValueEnd + 1, value.length);
connectStr += propertyValue + ";";
return MyRecursion(value, connectStr);
}
return connectStr;
}
//#endregion
var s = "<root><Property Name='长' Value='11' /><Property Name='宽' Value='22' /><Property Name='属性三' ID='9ee7e7bc-1cdc-47c3-b856-09873c7190b4' Value='E8E' /></root>";
var str = MyRecursion(s, "");
var s="<root><Property Name='长' Value='11' /><Property Name='宽' Value='22' /><Property Name='属性三' ID='9ee7e7bc-1cdc-47c3-b856-09873c7190b4' Value='E8E' /></root>";
var reg = /<[^>]*?Name\s*=\s*['"](.*?)['"][^>]*?Value\s*=\s*['"](.*?)['"][^>]*?>/g,arr=[];
while(reg.exec(s)!=null){
arr.push(RegExp.$1+'='+RegExp.$2);
}
alert(arr)