Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combinging rules in Regex

Trying to write a rule for a WP redirect

I’ve got the rule ^((?!json).)*$

Which ignores:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • /wp-json/aber

And the rule ^([^.\?]*[^\/])$

Which ignores:

  • /aber.css
  • /france?foobar=1

But I can’t seem to figure out how t combine them into a single rule that ignores all three. if I add [^.?|json] if rules out any URLs including any of those characters individually. It only needs to be exactly "json" located anywhere in the URL or URLs that include a . or a ?

Examples:

Dont match:

  • /wp-json/aberdeen
  • /aberdeen.css
  • /aberdeen?foobar=1

Match:

  • /wp-jsn/aberdeen
  • /aberdeen

>Solution :

Well the first pattern ^((?!json).)*$ simply asserts that json does not appear anywhere in the input. We can refactor the second pattern by adding a negative lookahead to rule out json:

^(?!.*json)([^.\?]*[^\/])$
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading