开发者

javascript regular expression match

开发者 https://www.devze.com 2023-02-08 20:02 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号