gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:02:54 26 4
gpt4 key购买 nike

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

矩阵(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com