开发者

Javascript regex match

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?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜