Regex to fix malformed vector

80 views Asked by At

I have an API that is returning the following, malformed string:

M0 0L1.33333 0L1.33333 4L4.44089e-16 4L0 0Z

It should look like this:

M 0 0 L 1.33333 0 L 1.33333 4 L 4.44089e-16 4 L 0 0 Z

How could I write a regex that would select any letter that has a number next to it, preceding or following?

Here are the strings again, next to each other:

M0 0L1.33333 0L1.33333 4L4.44089e-16 4L0 0Z <-incorrect

M 0 0 L 1.33333 0 L 1.33333 4 L 4.44089e-16 4 L 0 0 Z <-correct

Thanks!

A little background: this is a vector returned by the Figma API and I don't know why it's formatted like that.

2

There are 2 answers

1
Jacek Wyroba On

something like this should work: /[A-Z]\d|\d[A-Z]/g

https://regexr.com/ really helps with testing out regexes.

here's a link to the one i've been testing on: https://regexr.com/65vu7

0
Waldhuette On

You could use this regex here regexr.com/65vvt

([A-Z])\d|\d([A-Z])

And then check for capture group 1 or 2. That will match all the capital letters of your incorrect example string but none of your correct string.