2012-04-18 37 views
0

我需要重新組織一個陳舊的cvs數據庫,以便它可以使用正確的特殊字段名稱導入電子商務軟件。Coffeescript多行正則表達式替換混淆

這是我正在匹配的一些代碼。 [ID,類別,生產廠家,型號]

"1","3","Samsung","Galaxy S III" 
"2","3","HTC","One X" 

我的正則表達式

".*?".*?".*?".*?(".*?") 

基本上,我需要一個稍微更復雜的對象字面像

{manufacturers: 'Samsung'} 

只更換製造商,但我似乎無法弄清楚如何有選擇地取代第三場比賽。 我知道我可以分割字符串,並提取我需要的數據,但我不知道如何更換匹配,使輸出變量的樣子:

"1","3",{manufacturers: 'Samsung'},"Galaxy S III" 
"2","3",{manufacturers: 'HTC'},"One X" 

感謝您幫助我!

+0

你試過什麼代碼來取代匹配? – 2012-04-18 11:58:55

回答

2

匹配此:

"(.*?)","(.*?)","(.*?)","(.*?)" 

通過此替換:

"$1","$2",{manufacturers: '$3'},"$4" 

實施例:

var s='"1","3","Samsung","Galaxy S III"'; 
var p='"(.*?)","(.*?)","(.*?)","(.*?)"'; 
var r='"$1","$2",{manufacturers: \'$3\'},"$4"'; 
console.log(s.replace(new RegExp(p,"g"),r)); 

輸出:

"1","3",{manufacturers: 'Samsung'},"Galaxy S III" 
+0

我感激不盡。謝謝。 – youanden 2012-04-18 22:26:31