StJimmy
StJimmy

Reputation: 504

Javascript Regular Expression for CSS

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?

Upvotes: 0

Views: 289

Answers (4)

Umesh Patil
Umesh Patil

Reputation: 10705

$('el').css('-moz-transform').split(',')[4]

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

Upvotes: 1

Toto
Toto

Reputation: 91528

How about:

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

If you want to remove the spaces:

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

Upvotes: 2

Josh Mein
Josh Mein

Reputation: 28665

You could just do a split on the commas.

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

Upvotes: 0

ChrisR
ChrisR

Reputation: 14467

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];

Upvotes: 0

Related Questions