Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Regex to replace translate3d string from style string

I am trying to replace alla the occurences of the string translate3d(-640px, 0px, 0px); with translate(-640px).

This is what I am doing:

var string = "width: 960px; height: 548px; transition: 0s; -webkit-transition: 0s; transform: translate3d(-640px, 0px, 0px); -webkit-transform: translate3d(-640px, 0px, 0px);";
var regExp = //; //I am not able to create this one
var matches = regExp.exec(string);
var left = matches[1]; //this will give the value -640px
var newstring = string.replace('translate3d('+left+', 0px, 0px)', 'translate('+left+')');

There are 2 problems:

  1. I cannot create the right regex to only extract the left value.
  2. The .replace() method only replace one occurence of the string, while I need to replace them all.

Thanks for any help!

like image 566
Anonymous Avatar asked May 07 '26 01:05

Anonymous


1 Answers

You can use:

var newstring = string.replace(/translate3d\(([^,]+)[^)]*\)/g, 'translate($1)');

RegEx Demo

like image 56
anubhava Avatar answered May 09 '26 15:05

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!