开发者

Regular Expression - Get if there is just one

I would like a regex which

Grabs target from This:

http://localhost/target or http://localhost/target/

B开发者_运维技巧ut NOT from This:

http://localhost/target/something/else

So If there is just one parameter after localhost and not more than one.


This should get you going:

^.*target/?$


'http://localhost/target/'.match( /^http:\/\/localhost\/(target)\/?$/ )

would match

  • http://localhost/target
  • http://localhost/target/

And not match

  • http://localhost/target/f

You didnt specify whether the http matching was required, but if it isn't just remove that portion. This is a fairly primitive regex but should suffice.


/^http:\/\/localhost\/([^\/]+)\/?$/

Or if you can do a different delimiter like in Perl, to avoid LTS:

m|^http://localhost/([^/]+)/?$|
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜