2011-01-13 77 views
1
var string = "M-84.1487,-15.8513 a22.4171,22.4171 0 1 0 0,31.7026 h168.2974 a22.4171,22.4171 0 1 0 0,-31.7026 Z"; 
var regex = "[a-zA-Z][0-9, /-/.]*"; 
var array = string.match(regex); 

誰能幫助我與我的正則表達式匹配個人說明資訊(array[0] == "M-84.1487,-15.8513"; array[1] == "a22.4171,22.4171 0 1 0 0,31.7026";正則表達式在JavaScript中匹配SVG路徑數據

非常感謝

回答

2

按照BNF description可以識別作爲指令一封信,後面是任何不是字母的東西。

var pathData = "M-84.1487,-15.8513 a22.4171,22.4171 0 1 0 0,31.7026 h168.2974 a22.4171,22.4171 0 1 0 0,-31.7026 Z"; 
var pathSegmentPattern = /[a-z][^a-z]*/ig; 
var pathSegments = pathData.match(pathSegmentPattern); 

當然,您可能想要trim的結果,但這不應該太難。

也儘量不要給你的變量命名如此無意義(string,regex,array)。在我看來,這比命名他們abc更糟糕。

+1

謝謝,我會試試這個。變量只是爲了這篇文章而命名:) – Tom 2011-01-13 15:45:30