简体   繁体   中英

Regular expression to parse url

I am trying to get a regular expression in Javascript where:

/something/:workspaceId/somethingelse/:userId/blah/:id

Is parsed so that I get an array with ['workspaceId', 'userId', 'id'] .

Initial test:

p.match(/:.*?\/+/g )

This only matches the first two, workspaceId and userId.

This feels like a bit of a hack:

(p + '/').match(/:.*?\/+/g )

And it still returns :workspaceId rather than workspaceId .

Trying to fix it with whatI think the brackets should be for:

(p + '/').match(/:(.*)?\/+/g )

This last one really ought to work, but won't (since there are brackets, I expect the regexp only to return the match in the brackets).

At the moment, I am doing:

r = (p + '/').match(/:.*?\/+/g).map( function(i){return i.substr(1, i.length - 2 )  } );

But I would love to get something that:

1) Doesn't add that '/' at the end (although I could live with it)

2) It doesn't use the expensive map() method to do something the regexp should be doing in the first place

1: You need to escape the forward-slash like so:

\/something\/:workspaceId\/somethingelse\/:userId\/blah\/:id

2: Add capturing groups to capture the things you need, like so:

\/something\/:(.*?)\/somethingelse\/:(.*?)\/blah\/:(.*?)

3: Put the entire thing within javascript Regexp delimiters, /.../ :

/\/something\/:(.*?)\/somethingelse\/:(.*?)\/blah\/:(.*?)/

4: execute the regex

var rexp = /\/something\/:(.*?)\/somethingelse\/:(.*?)\/blah\/:(.*?)/;
var matched = rexp.exec(string_to_match);

You will have:

matched[0] => entire matched string
matched[1] => first capturing group, workspaceId
matched[2] => second capturing group, userId
matched[3] => third capturing group, id

5: Learn regex from a good online source

EDIT To make it even more generic, use this:

var rexp = /\/.*?\/:(.*?)\/.*?\/:(.*?)\/.*?\/:(.*?)/;

you could use a look ahead assertion regex for this, like so:

(?=:):(.*?)(?=\/|$)

working example:

http://regex101.com/r/gO7nO0

Matches:

MATCH 1
1.  
`workspaceId`
MATCH 2
1.  
`userId`
MATCH 3
1.  
`id`

Or better yet, you could simplify and just use:

:(\w+)

working example:

http://regex101.com/r/nG4mI2

Matches:

MATCH 1
1.  
`workspaceId`
MATCH 2
1.  
`userId`
MATCH 3
1.  
`id`

Edit:

Here is a working pure javascript example:

http://jsfiddle.net/BkbrF/2/

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