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 '

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

how can i get rid of the extra leading and trailing double quotes on the output?
maybe, use string replacement later .replace(/"/g,"") for each element of array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.