Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'-router. 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!

like image 990
soren.qvist Avatar asked Feb 04 '26 05:02

soren.qvist


2 Answers

You could try

^((?!ajax).*)
like image 101
Robusto Avatar answered Feb 05 '26 23:02

Robusto


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.

like image 39
gnarf Avatar answered Feb 05 '26 22:02

gnarf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!