1

I've got this css string

var cssString = 'scale(0.95,0.95) rotate(45deg) translate(10px,11px)';

I want to use regex to return an array looking like this:

var array = ['scale(','0.95',',','0.95',') rotate(','45','deg) translate(','10','px,','11','px)'];

my last attempt:

var array = cssString.match(/([ a-zA-Z])*(\d*[.]?\d*)*/g);

I'm struggeling to have the first group match the parantheses aswell. How do I make this happen?

Clemens Himmer
  • 1,340
  • 2
  • 13
  • 26
Puddingboy
  • 127
  • 9

1 Answers1

0

This regex will separately select both text (with special characters ()) and numbers (with dots .) and the commas:

([A-Za-z()])+|(,)+|([.\d])+
Filipe
  • 1,788
  • 2
  • 13
  • 18
  • It doesn't return this tho: `) rotate(` If you would look at my array? – Puddingboy Jan 15 '16 at 11:11
  • My bad. Try this: `([A-Za-z\s()])+|(,)+|([.\d])+` – Filipe Jan 15 '16 at 11:12
  • I'm sorry, your current example did indeed work before my last edit. I made a mistake in that return value. The idea is that I can iterate over it with a for loop and use an even/odd principle to retrieve numbers or other. Can you take a look at it again? – Puddingboy Jan 15 '16 at 11:17
  • 1
    Sure. Use this: `([\sA-Za-z(),])+|(,)+|([.\d])+` – Filipe Jan 15 '16 at 11:35
  • I have a few questions regarding regex. Can abduct you to a [chat]? – Puddingboy Jan 15 '16 at 12:41
  • I'm unable to use [Stack Overflow Chat](http://chat.stackoverflow.com/) it requires proxy authentication. For now, i'm new here. – Filipe Jan 15 '16 at 12:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100764/discussion-between-puddingboy-and-filipe-costa-meneses). – Puddingboy Jan 15 '16 at 12:43
  • I was just wondering, considering your placement of the `,` in the first group. Does this not mean the second group could be removed? The new regex would look like this I suppose `([\sA-Za-z(),])+|([.\d])+` – Puddingboy Jan 18 '16 at 10:45
  • That will do it, but it also will merge `,` to any `\sa-zA-Z()`. So it wont produce: `'scale(0.95,0.95)'` => `['scale(','0.95',',','0.95',')']`. You can check it right [here](http://regexr.com/3cjml) – Filipe Jan 18 '16 at 10:56
  • What do you mean? The idea was to only have 2 groups, nums(float included) and non-nums. `/([\sA-Za-z(),])+|([.\d])+/g` would do just fine to do this or? I don't think any `,` exists outside of `(...)` and inside, they are always followed by a number. Where do you suspect issues? – Puddingboy Jan 18 '16 at 12:12