Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:31:36+00:00 2026-06-14T15:31:36+00:00

Given a style attribute string, I need to capture the x and y values

  • 0

Given a style attribute string, I need to capture the x and y values of a CSS translate() property. Numbers may be zero, positive or negative, float or integer, and only of pixel units.

A practical style attribute string example:

min-height: 100%;
min-width: 100%;
transition-property: transform;
transform-origin: 0px 0px;
transform: translate(123px, 0px) translateZ(0px);

I have a working, albeit very ugly solution. Here’s a breakdown:

var transform = element.style['transform'] – transform variable then contains this string: "translate(123px, 0px) translateZ(0px)"

Then I match it against:

transform = transform.match(/translate\(-?[0-9]+(\.[0-9]*)?px, -?[0-9]+(\.[0-9]*)?px\)/);

I do this so if there’s another transform property, like rotate(20px, 20px) for example, I can avoid it.

The regex produces this array: ["translate(123px, 0px)", undefined, undefined].

Another regex is then in order, to match the numeric values only:

transform = transform[0].match(/(-?[0-9]+(\.[0-9]*)?)px, (-?[0-9]+(\.[0-9]*)?)px/);

Then I am left with the following array: ["123px, 0px", "123", undefined, "0", undefined]

And to finish everything I need to parseInt(transform[1]) and parseInt(transform[3]) to actually work with the values.

This seems like too much work for getting only those two numbers, but I am not very experienced with regular expressions, and I do not know how to streamline this. Is there a more elegant solution to this?

EDIT: I will particularly be happy to turn it all into a single regular expression 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:31:37+00:00Added an answer on June 14, 2026 at 3:31 pm

    You can combine the two steps into one by doing something like the following:

    transform = transform.match(/translate\((-?\d+(?:\.\d*)?)px, (-?\d+(?:\.\d*)?)px\)/);
    

    or if you trust your input you can do:

    transform = transform.match(/translate\((.*)px, (.*)px\)/);
    

    to simplify your regex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to change more than one style attribute for a given element. I
I have a string of style transform given in the following way : matrix(0.312321,
I am adding a css style to all occurrences of a given word in
I've got a tr:table that I need to style using CSS. All the normal
I know that it's not possible to style any given PHP but I do
Given a class Foo (whether it is a new-style class or not), how do
Given IE6, an UL-LI list and a background image for the UL container. <style>
i try to givee style via GridView.css into Masterpage my masterpage includes 3 pages
Given this simplified snippet : <html> <body> <div> <div style='text-align:center;'>asdfaskjdfakjsd</div> <div style='float:right'> <input type='submit'
Given this XML, how can I retrive the HEX color? <group> <span style=color:#DF0000; font-style:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.