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";
)非常感谢
最佳答案
根据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
)。我认为这比命名它们a
,b
或c
更糟。关于javascript - 正则表达式以匹配javascript中的SVG路径数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4681800/