Regular Expressions 101

Community Patterns

Remove multiple forward slash from url

1

Regular Expression
PCRE2 (PHP >=7.3)

~
([^:])(/{2,})
~
g

Description

([^:]): This is a capturing group that matches any character except a colon :, and it captures this character for later use in the replacement. (/ {2,}): This part matches two or more consecutive forward slashes. The replacement part $1/ uses the captured character from the first part (which is any character except a colon) followed by a single forward slash. This effectively replaces multiple consecutive forward slashes with a single slash, excluding cases where the slashes appear after a colon.

Here's a breakdown of how it works:

([^:]): Match any character that is not a colon and capture it. (/ {2,}): Match two or more consecutive forward slashes. $1/: Replace the matched pattern with the captured character followed by a single forward slash. This regular expression is useful for cleaning up URLs by removing redundant consecutive forward slashes, except those immediately following a colon.

Submitted by Apon Ahmed - a month ago (Last modified a month ago)