Regex for Removing a Trailing Slash From a URL
Matches a single trailing slash at the very end of a string.
Published October 13, 2026
This regex matches one or more slashes at the end of a string so you can strip them.
How the pattern works
Breaking the pattern down: /+ = one or more slashes; $ = at the end.
Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.
Pattern tests
//+$/
✓ 'https://example.com/path/'
✓ '/a//'
✗ 'https://example.com/path'
Practical examples
The pattern
const re = //+$/Test it
const re = //+$/
re.test('https://example.com/path/') // true
re.test('/a//') // true
re.test('https://example.com/path') // falseIn Python
import re
re.search(r'/+$', text)Common mistakes
- Stripping the slash from the root URL.
- Forgetting query strings.
- Needing consistent redirects for SEO.
Key takeaways
- Anchors (^ and $) make the whole string match instead of a part of it.
- Regex checks format only; verify data separately when it matters.
- Test with valid and invalid samples.
FAQ
Keep root '/'?
Only strip when the path is longer than '/'.
Where can I test this pattern?
Use the Regex Tester on this site, or regex101.com.