I'm working on a function to parse a text field. Below are some scenarios:
In: "about"
Result: var keywords = "about"
In: "type:page " (notice the space)
Result: var types = ["page"];
In: "type:page about"
Result: var types = ["page"], keywords = "about";
In: "type:page,event The Event"
Result: var types = ["page", "event"], keywords = "The Event";
Can someone point me in the right direction as to how I'd parse this using RegE开发者_如何学Pythonx?
function inOut (input) {
var output = {};
if (input.indexOf('type:') !== -1) {
output.types = input.replace(/^.*type:([^ ]+).*$/, '$1').split(',');
output.keywords = input.replace(/^(.*)type:([^ ]+)(.*)$/, '$1 $3');
} else {
output.keywords = input;
}
return output;
}
Try this?
精彩评论