-3

I'm trying to extract the degree rate from the CSS transform property,

transform = "rotate(33.8753deg) translateZ(0px)"

with a regular expression. So far I've succeeded to get almost the exact number:

const re = new RegExp('.*rotate( *(.*?) *deg).*', 'm');
let degRate = transform.match(re);

Output: An array which the third element is:

"(33.8753"
  1. How can I get only the number without the parenthesis?
  2. How can I get only the number? (not in an array)
TylerH
  • 20,799
  • 66
  • 75
  • 101
Yinon
  • 945
  • 1
  • 8
  • 21

2 Answers2

1

You can use the RegEx \(([^(]*)deg\) and get the first group with .match(...)[1]

  • \( matches the first (

  • ([^(]*) captures anything but ( 0 or more times

  • deg\) matches deg) literally.

let str = "rotate(33.8753deg) translateZ(0px)";
let deg = str.match(/\(([^(]*)deg\)/)[1];
console.log(deg);
Zenoo
  • 12,670
  • 4
  • 45
  • 69
  • this will work with the actual string but am pretty sure he want to use it with real style and it won't work as we will get the matrix transformation – Temani Afif Mar 19 '18 at 09:19
  • @TemaniAfif I don't really follow you, can you elaborate please? – Zenoo Mar 19 '18 at 09:21
  • actually we are considering a simple string to get the value of degree ... but am sure at the end we will read the transform property of an element and he *maybe* think that we will get the same string but not we will get the matrix transformation – Temani Afif Mar 19 '18 at 09:24
1

Simpler extraction:

let str = "rotate(33.8753deg) translateZ(0px)";
let deg = parseFloat(str.replace(/^.*rotate\(/,""));
console.log(deg);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • this will work with the actual string but am pretty sure he want to use it with real style and it won't work as we will get the matrix transformation – Temani Afif Mar 19 '18 at 09:19