javascript regular expression match
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.
[^"\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.
([^\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+[:;\.,\?!]?
This works, at least for your two cases:
((?:[^\s]*\"[^\"]+\")|[\w:]+)
see here
精彩评论