3

I need a help on this string parsing. What i want to do is to parse a string like this one using Javascript.

"myname" secondarg "third argument" andso on

to give me a result like

[0] => myname
[1] => secondarg
[2] => third argument
[3] => andso
[4] => on

EDIT:

"test" " foo" bar "hello world" " hello "

to

[0] => 'test'
[1] => ' foo'
[2] => 'bar'
[3] => 'hello world'
[4] => ' hello '
razorxan
  • 516
  • 1
  • 7
  • 20

1 Answers1

3
function argsFrom(string) {
    return string.match(/'[^']*'|"[^"]*"|\S+/g) || [];
}

That is pretty decent, but it doesn't deal with escaped quotation marks, etc. It's pretty complicated to have a complete solution, just as complicated as parsing CSV.

Javascript code to parse CSV data

Community
  • 1
  • 1
Joseph Myers
  • 6,434
  • 27
  • 36