用于从变换矩阵中选择元素的正则表达式

mik*_*eck 6 javascript css regex arrays css3

我有一个以下列方式给出的样式转换字符串:

matrix(0.312321, -0.949977, 0.949977, 0.312321, 0, 0)

如何形成包含此矩阵元素的数组?有关如何为此编写正则表达式的任何提示?

Bil*_*oon 7

我会这样做的......

// original string follows exactly this pattern (no spaces at front or back for example)
var string = "matrix(0.312321, -0.949977, 0.949977, 0.312321, 0, 0)";

// firstly replace one or more (+) word characters (\w) followed by `(` at the start (^) with a `[`
// then replace the `)` at the end with `]`
var modified = string.replace(/^\w+\(/,"[").replace(/\)$/,"]");
// this will leave you with a string: "[0.312321, -0.949977, 0.949977, 0.312321, 0, 0]"

// then parse the new string (in the JSON encoded form of an array) as JSON into a variable
var array = JSON.parse(modified)

// check it is correct
console.log(array)
Run Code Online (Sandbox Code Playgroud)