StueyKent
StueyKent

Reputation: 347

Regex to split minifiyed SVG Path

Due to my poor Regex knowledge I can't seem to work this one out.

I have a string and want to split it down into an array of real number strings.

For example c15.1-5.3.1-.1.1-.2.1 would become ["15.1", "-5.3", ".1", "-.1", ".1", "-.2", ".1"]

I have the following code which splits on spaces and minus' but doesn't handle the double decimal. Any thoughts?

var path = 'c15.1-5.3.1-.1.1-.2.1'
var a = path.slice(1, path.length).split(/(?=[ -])/)

This is the output: ["15.1", "-5.3.1", "-.1.1", "-.2.1"]

Upvotes: 1

Views: 548

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

Use string.match instead of string.split

var path = 'c15.1-5.3.1-.1.1-.2.1';
console.log(path.match(/-?\d*(\.\d+)?/g).filter(function(n){ return n != '' }));

Upvotes: 3

Related Questions