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

前端之家收集整理的这篇文章主要介绍了javascript – 用于从变换矩阵中选择元素的正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个以下列方式给出的样式转换字符串:

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

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

解决方法

我会这样做的……
  1. // original string follows exactly this pattern (no spaces at front or back for example)
  2. var string = "matrix(0.312321,0)";
  3.  
  4. // firstly replace one or more (+) word characters (\w) followed by `(` at the start (^) with a `[`
  5. // then replace the `)` at the end with `]`
  6. var modified = string.replace(/^\w+\(/,"[").replace(/\)$/,"]");
  7. // this will leave you with a string: "[0.312321,0]"
  8.  
  9. // then parse the new string (in the JSON encoded form of an array) as JSON into a variable
  10. var array = JSON.parse(modified)
  11.  
  12. // check it is correct
  13. console.log(array)@H_404_11@

猜你在找的JavaScript相关文章