使用javascript解析SVG变换属性 [英] Parse SVG transform attribute with javascript
本文介绍了使用javascript解析SVG变换属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有一个SVG转换字符串:
Supposing we have an SVG transform string:
transform = "translate(6,5),scale(3,3)";
是否有一个光滑的正则表达式函数可用于将其解析为可用的东西?
Is there a slick regex function we could use to parse that into something usable?
推荐答案
这是一个漂亮的小片段代码,可能会满足您的需要,它应该涵盖大多数情况,以防您要解析的字符串具有不同数量的参数:
Here's a nifty little snippet of code that might get you what you need, it should cover most scenario in case the string you intend to parse has different number of arguments:
function parse (a)
{
var b={};
for (var i in a = a.match(/(\w+\((\-?\d+\.?\d*e?\-?\d*,?)+\))+/g))
{
var c = a[i].match(/[\w\.\-]+/g);
b[c.shift()] = c;
}
return b;
}
运行此
parse('translate(6,5),scale(3,3.5),a(1,1),b(2,23,-34),c(300)');
会导致以下结果:
{
translate: [ '6', '5' ],
scale: [ '3', '3.5' ],
a: [ '1', '1' ],
b: [ '2', '23', '-34' ],
c: [ '300' ]
}
这篇关于使用javascript解析SVG变换属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文