Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于从变换矩阵中选择元素的正则表达式_Javascript_Regex_Arrays_Css - Fatal编程技术网

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

Javascript 用于从变换矩阵中选择元素的正则表达式,javascript,regex,arrays,css,Javascript,Regex,Arrays,Css,我用以下方式给出了一系列样式转换: 矩阵(0.312321,-0.949977,0.949977,0.312321,0,0) 如何形成包含此矩阵元素的数组?有关于如何为此编写正则表达式的提示吗?试试以下方法: /^matrix\(([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+)\)$/ .exec(str).slice(1); 我会这样做 // original string fol

我用以下方式给出了一系列样式转换:

矩阵(0.312321,-0.949977,0.949977,0.312321,0,0)

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

试试以下方法:

/^matrix\(([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+), ([+\-\d.]+)\)$/
    .exec(str).slice(1);

我会这样做

// 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)

可能是这样的:

var string = "matrix(0.312321, -0.949977, 0.949977, 0.312321, 0, 0)";

var array = string.replace(/^.*\((.*)\)$/g, "$1").split(/, +/);
注意,通过这种方式,数组将包含字符串。如果您想要实数,一个简单的方法是:

array = array.map(Number);

您的js引擎需要支持或有一个垫片(当然您也可以手动转换它们)。

这里有一种方法。用正则表达式解析出数字部分,然后使用
split()
方法:

var s = "matrix(0.312321, -0.949977, 0.949977, 0.312321, 0, 0)";
s.match(/[0-9., -]+/)[0].split(", "); // results in ["0.312321", "-0.949977", "0.949977", "0.312321", "0", "0"]

不使用
e-
示例:
matrix(6.12323e-17,-0.949977,0.949977,0.312321,0,0)
不使用
e-
矩阵(6.12323e-17,1,-1,6.12323e-17,0,0)Thx,但这对我来说已经足够好了:
JSON.parse(`[${string.slice(7,-1)}}