开发者

How do I catch strings that don't start with "ajax/" using regex for CodeIgniter?

开发者 https://www.devze.com 2023-01-08 19:14 出处:网络
I\'m working on some routes for my CodeIgniter application, and I need to declare a \'catch-all\'/except one regular expression. Any route that doesn\'t start with \'ajax/\' should be redirected to th

I'm working on some routes for my CodeIgniter application, and I need to declare a 'catch-all'/except one regular expression. Any route that doesn't start with 'ajax/' should be redirected to the 'main'-rou开发者_运维技巧ter. Like so:

$route['regexmagichere'] = "main";

So this is definetly way beyond my regex skills and I need some help. The regex should return true on all strings that don't start with 'ajax/', like so:

$string_one = "ajax/someotherstuffhere";
$string_two = " ajax/test";
$string_three = "somestuffhere";

Here $string_one would be the only one returning false. Thanks for your time!


You could try

^((?!ajax).*)


To be litereal to your request. A regexp that returns true for all strings that don't start with ajax/:

^(?!ajax/).*

You might need to escape the / as \/. (?!) is a negative look-ahead expression explained on this question.


I think ^(?<!ajax)\/ will do the trick.

I stress "think"!

0

精彩评论

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