0

I'm trying to extract a value through Javascript from an element's CSS3 attribute such as the following:

CSS:

el {
  -moz-transform: matrix(1, 0, 0, 1, 27px, 0px);
}

Javascript (jQuery)

var style = $('el').css('-moz-transform');

The style variable takes the following string when the script runs: "matrix(1, 0, 0, 1, 27px, 0px)"

I'd like to extract the number corresponding to the 27px field inside that string.

I imagine the fastest, or at least most elegant way to do so is with regular expressions, although I don't really know how to use them in JS.

Any suggestions?

4 Answers 4

2

How about:

var value = style.split(',')[4];

If you want to remove the spaces:

var value = style.split(/, */)[4];
Sign up to request clarification or add additional context in comments.

1 Comment

What about the spaces between the commas? Well, I suppose if you use parseInt, the spaces won't matter.
1
$('el').css('-moz-transform').split(',')[4]

Please, Test the below jsfiddle in Mozilla firefox -moz-transform is used only in Firefox.

2 Comments

The force is strong with this one.
Thanks. Don't know how I didn't think about that before .
0

IMO the easiest way to do this is actually just splitting the string on comma's and getting the 5th parameter, no need to fire up the regex engine

var val = style.split(',')[4];

Comments

0

You could just do a split on the commas.

var style = $('el').css('-moz-transform').split(",")[4];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.