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

标签 javascript regex arrays css

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

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

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

最佳答案

我会这样做...

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

关于javascript - 用于从变换矩阵中选择元素的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11236090/

相关文章:

javascript - 获取页面上包含的外部脚本的内容

javascript - Stripe 支付工作流程(如果使用 Connect-API,有什么变化吗?)

c# 试图将第一个字母更改为大写但不起作用

arrays - ruby 中数组的 &.each 和 .each 之间的区别?

javascript - 如果我在 Windows 控制台中传递此代码,它可以工作,但是当我在 node.js 代码中模拟 Windows 控制台时,它不起作用,并返回不清楚的错误

javascript - 连续显示 4 个标签

regex - 根据子字符串出现在字符串中的位置识别字符串

mysql - 请使用正确的正则表达式语法提供建议

Java > Array-2 > ZeroMax

javascript计算对象数组中没有空值的对象