Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Regex for selecting elements from transform matrix

Regex for selecting elements from transform matrix

Problem

I have a String of style transform given in the following way :

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

How can I form an array containing elements of this Matrix ? Any tips how to write regex for this ?

Problem courtesy of: mike_hornbeck

Solution

I would do it like this...

// 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)
Solution courtesy of: Billy Moon

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Regex for selecting elements from transform matrix

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×