开发者

Regular Expression - Get if there is just one

开发者 https://www.devze.com 2023-01-31 16:19 出处:网络
I would like a regex which Grabs target from This: http://localhost/target or http://localhost/target/

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

精彩评论

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