刪除號碼我有一個字符串:正則表達式和Javascript從字符串
str = 'View:{
Name:"View1",
Image:{
BackgroundImage:"Image.gif",
Position: [0, 0],
Width: 320,
Height: 480
},
Button:{
BackgroundImage:"Button.gif",
Transition:"View2",
Position: [49, 80],
Width: 216,
Height: 71
},
Button:{
BackgroundImage:"Button2.gif",
Position: [65, 217],
Width: 188,
Height: 134
},'
,我用這個正則表達式來添加「_#」到具有元素「:{」他們
結束var i = 0;
str = str.replace(/([^:]+):{/g, function(m, p1) { return p1 + "_" + (++i).toString() + ":{"; });
的輸出中是
str = 'View_1:{
Name:"View1",
Image_2:{
BackgroundImage:"Image.gif",
Position: [0, 0],
Width: 320,
Height: 480
},
Button_3:{
BackgroundImage:"Button.gif",
Transition:"View2",
Position: [49, 80],
Width: 216,
Height: 71
},
Button_4:{
BackgroundImage:"Button2.gif",
Position: [65, 217],
Width: 188,
Height: 134
},'
然後我做了一堆用它的東西,現在我需要從中剝離出「#」。我將如何去除這些'#'
不過關,但我遇到的另一個問題是,第一個正則表達式是從0開始遞增,並給每個元素下一個遞增的數字。我正在努力使每個元素的類型都增加。 像這樣:
str = 'View_1:{
Name:"View1",
Image_1:{
BackgroundImage:"Image.gif",
Position: [0, 0],
Width: 320,
Height: 480
},
Button_1:{
BackgroundImage:"Button.gif",
Transition:"View2",
Position: [49, 80],
Width: 216,
Height: 71
},
Button_2:{
BackgroundImage:"Button2.gif",
Position: [65, 217],
Width: 188,
Height: 134
},'
上什麼IM任何輸入做錯了嗎?
你得到一個語法錯誤arent你嗎? – Ibu
爲什麼不修正構建這個無效的JSON對象的東西:)看起來應該是構建一個對象數組。 – epascarello
爲什麼你需要數字獨立增量?你擔心數字用完嗎? :) – Barmar