Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lkmadushan's avatar

Laravel regex issue validating url with or without htttp:// or https://

'url'   => 'regex:/^((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)$/'

This regex raises an error "No ending delimiter / found", but this code works

preg_match("/^((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)$/i", "http://google.com");
0 likes
1 reply
gildniy's avatar

Yeah it does because you have "|" character in the regex rule, to make that work you should use an array instead like this:

'url'   => ['regex' => '/^((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)$/'],
5 likes

Please or to participate in this conversation.