2012-12-27 102 views
2

我想將一個字符串分解成一個數組。下面的代碼確實做到了,但我現在需要將它分解成一個子數組。使用.split()將一個字符串分解成一個數組

這裏是字符串並將代碼:

$(document).ready(function() { 
var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}'; 

var result = content.split('}'); 
result.pop();// removing the last empty element 
console.log(result); 
for(var i=0;i<result.length;i++) 
{ 
    result[i]+='}'; 
    console.log(result); 
     $('div').append('<li>' + result[i] + '</li>'); 
} 
}) 

這出看跌期權:

<li>Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}</li> 
<li>Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}</li> 
<li>Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}</li> 
<li>Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}</li> 

我現在需要做的是進一步打破它讓我有之前的字{}即圖片爲第一個

理想我想輸出是一個鍵/值對象這樣

{ 
     "Controls": [{ "Image":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
    "Button":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
"Button":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
    "Label":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", }], 
    } 

我的主要目標是能夠以此爲目標的關鍵或價值。

任何幫助表示讚賞

+0

[你嘗試過什麼?](http://www.whathaveyoutried.com) –

+1

那是哪裏字符串來自哪裏?爲什麼這種格式? – elclanrs

+0

字符串從另一個程序傳遞給我,我需要解析/分解成可以操縱的東西,即目標和修改'高度'或'x'等。我使用Split()函數來獲取此遠,但不知道如何在保留所有數據的同時進一步細分。 – Rob

回答

0

這將產生你想要的輸出:

$(document).ready(function() { 
    var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}'; 

    var result = content.split('}'); 
    result.pop();// removing the last empty element 
    var obj = {Controls:{}}; 

    $.each(result, function (i, v) { 
     var key = v.split('{'); 
     var value = v.replace(key[0], '') + '}'; 

     if (key[0] !== 'Button') { 
      obj.Controls[key[0]] = value; 
     } else { 
      if (!obj.Controls.hasOwnProperty('Buttons')) { 
       obj.Controls['Buttons'] = []; 
      } 
      obj.Controls.Buttons.push(value); 
     } 
    }); 

    console.log(obj); 
});​ 

工作演示:http://jsfiddle.net/fewds/TuNTV/2/

輸出例:

{ 
    "Controls": { 
     "Image": "{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
     "Buttons": [ 
      "{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}", 
      "{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}" 
     ], 
     "Label": "{Position: 106, 91;Width: 96;Height: 34;Text: \"Button\";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}" 
    } 
} 

如果你只是想有多個關鍵事件基準遞增,你可以這樣做:

$(document).ready(function() { 
    var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Image{BackgroundImage: Image2.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Button{BackgroundImage: Button3.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}'; 

    var result = content.split('}'); 
    result.pop();// removing the last empty element 
    var obj = {Controls:{}}; 

    function nextProp(key) { 
     if(obj.Controls.hasOwnProperty(key)) { 
      var num = key.match(/\d+$/); 
      if (num) { 
       return nextProp(key.replace(num[0], '') + (parseInt(num[0], 10) + 1)); 
      } else { 
       return nextProp(key + '1'); 
      } 
     } 

     return key; 
    } 

    for (var i = 0; i < result.length; i++) { 
     var key = result[i].split('{'); 
     var value = result[i].replace(key[0], '') + '}'; 
     obj.Controls[nextProp(key[0])] = value; 
    } 

    console.log(obj); 
});​ 

工作演示:http://jsfiddle.net/fewds/TuNTV/5/

輸出例如:

{ 
    "Controls": { 
     "Image": "{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", 
     "Image1": "{BackgroundImage: Image2.gif;Position: 0, 0;Width: 320;Height: 480;}", 
     "Button": "{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}", 
     "Button1": "{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}", 
     "Button2": "{BackgroundImage: Button3.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}", 
     "Label": "{Position: 106, 91;Width: 96;Height: 34;Text: \"Button\";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}", 
     "Label1": "{Position: 106, 91;Width: 96;Height: 34;Text: \"Button\";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}" 
    } 
} 
+0

而不是嵌套'按鈕',我們可以只分配一個索引,即按鈕[2]或'按鈕2'?問題是我會得到可能有多個控件(圖像,按鈕等)的巨大字符串。這將是我甚至可能得到像這個scrollview(w:1,x:2,y:3 {Button {x:4,y:5,w:6}}這樣的嵌套項目的時候。關於這一點,將不勝感激 – Rob

+0

@Rob我爲你增加了另一個例子,這一個將增加鍵來說明多個關鍵事件 – PhearOfRayne

+0

這是非常有幫助的對於noob問題抱歉,但我會如何定位這些項目中的任何一個?我只是想返回'圖像',或者如果我想返回'圖像:「{所有的東西}」?我試過obj.Button但得到undefined。謝謝 – Rob

0

使用正則表達式可能會更好

// the regexp 
var re = /(.+?)(\{.*?\})/g, 
    group; 

var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}'; 

var ret = {}; 

while(group = re.exec(content)) { 
    var k = group[1], 
     v = group[2]; 

    ret[k] = ret[k] ? ret[k].substring(0, ret[k].length - 1) + v.substring(1, v.length) : v; 
} 


// output 
console.log(ret); // => {'Button': '{xxx}', 'Image': '{xxxx}', 'Label': '{xxxx}'} 
+0

謝謝。我會放棄它。 – Rob

0

試試這個。如果您想要以任何關鍵值爲目標,您可以將其解析爲JSON。但我認爲這是無效的JSON。

var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}' + 
'Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}' + 
'Button2{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}' + 
'Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}'; 

// add colons after the keys 
content = content.replace(/([\w]+){/g, '"$1":{'); 

// wrap all {} in quotes 
content = content.replace(/({[^}]+})/g, '"$1",'); 

// remove trailing comma 
content = content.slice(0, -1); 

content = '{"Controls": [{' + content + '}]}'; 

console.log(content); 
相關問題