1

I need to split a string like the one below, based on space as the delimiter. But any space within a quote should be preserved. There are two cases which needs to work

Case 1

research library "not available" author:"Bernard Shaw"

to

research 
library 
"not available" 
author:"Bernard Shaw" 

Case 2

research library "not available" author:Bernard

to

research 
library 
"not available" 
author:Bernard 

I am trying to do this with Javascript and regular expression.

var splitArray = query_string.match(/([^\s]*\"[^\"]+\")|\w+/g);

Case 1 works as required but Case 2 produces the result as below

research 
library 
"not available" 
author
Bernard 

I need both the cases to work with one Regex. Any ideas appreciated.

3 Answers 3

1
[^"\s]+(?:"[^"]+")?|"[^"]+"

Explanation:

[^"\s]+       # One or more non-space/non-quote characters
(?:"[^"]+")?  # optionally followed by a quoted string
|             # or
"[^"]+"       # just a quoted string.

Assuming that there are no escaped quotes within quoted strings.

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

1 Comment

Thanks Tim. It works great. Looks to me the most comprehensive of all the answers.
1
([^\s]*\"[^\"]+\")|\w+:?

I've tested this regex here: rubular

update: you may want to include some more punctuation marks like ; , . ? !
e.g. research library! "not available" author:"Bernard Shaw" test1, test2; test2!

([^\s]*\"[^\"]+\")|\w+[:;\.,\?!]?

1 Comment

thanks. it gives "author:" and "Bernard" separately when I try to put it in use. In Rubular, seems to work well though.
0

This works, at least for your two cases:

((?:[^\s]*\"[^\"]+\")|[\w:]+)

see here

Comments

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.