Select multiple elements in a regular expression

匆匆过客 提交于 2019-12-13 17:33:14

问题


I've got the URLs in the following style:

http://whatever.com/param1/val1/param2/val2

I want to match all key/value pairs. I tried this pattern:

/^http:\/\/whatever.com(?:\/([^\/]+)\/([^\/]+))*$/g

It only matches the last key/value pair.

Unfortunately, I cannot use code to get the pairs... How can I capture all pairs?


回答1:


Try making your match non-greedy by adding a ? after the *:

/^http:\/\/whatever\.com\/(?:([^\/]+)\/([^\/]+)\/?)*?$/g


来源:https://stackoverflow.com/questions/7187909/select-multiple-elements-in-a-regular-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!