-1

I would like to split the following string at each letter using String.prototype.split(/[a-zA-Z]/):

'M 10 10L 150 300'

The result:

[ "", " 10 10", " 150 300" ]

But I want to receive this:

[ "", "M 10 10", "L 150 300" ]


What is the fastest way to get that result using JavaScript?
jak.b
  • 273
  • 4
  • 15

1 Answers1

1

Try use match with /[a-zA-Z][^a-zA-Z]*/g to capture a letter and the following non letter characters:

let s = 'M 10 10L 150 300'

console.log(
  s.match(/^[^a-zA-Z]+|[a-zA-Z][^a-zA-Z]*/g)   // ^[^a-zA-Z]+ to include the case when 
                                               // the string doesn't begin with a letter
)
Psidom
  • 209,562
  • 33
  • 339
  • 356