简体   繁体   中英

How to match search terms within quotes

I am trying to parse a search term. The search term is one long string such as:

label:none author:@root label:~"To+Do" label:~'"test me"' label:~"No Label" label:~'won"t fix' this is a search term

I am trying to parse it into individual key value pairs:

[{label: "none"}, {author:"@root"}, {label:"~\"To+Do\""}, {label: "~"test me\""}, {label: "~\"No Label\""}, {label: "~won\"t fix"}, {search: "this is a search term"}]

The search term is the last item without a key. If the search terms have spaces they are wrapped in quoted.

I started to parse this by doing:

(label:.*?|author:.*?|milestone:.*?)

I am not sure if that will work, but I am also kind of confused on how to get the last search term. Not even sure if this is possible.

The biggest problem is that things are quoted and I am not sure how to match things that are quoted. In the end what I am trying to do is split a string by spaces while ignoring the quotes surrounding spaces.

I use @ for author, ~ for label, % for milestone.

This seems to work... I also converted the output struct to something easier to work with:

 term = `label:none author:@root label:~"To+Do" label:~'"test me"' label:~"No Label" label:~'won"t fix' this is a search term` re = /(\\w+):([~%@]?)(?:"(.*?)"|'(.*?)'|(\\S+))/g; opts = []; term = term.replace(re, function (_, prop, operator, v1, v2, v3) { opts.push({prop, operator, value: v1 || v2 || v3}); return ''; }); opts.push({search: term.trim()}); console.log(opts); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM