Commit 17db1024 authored by Jordan Frankfurt's avatar Jordan Frankfurt Committed by GitHub

fix: update url regex to correctly match URLs (#6220)

* fix: update url regex to correctly match URLs

* add test

* more test cases and better comment explanation
parent e8ca84ee
...@@ -3,4 +3,13 @@ import { hasURL } from './urlChecks' ...@@ -3,4 +3,13 @@ import { hasURL } from './urlChecks'
test('hasURL', () => { test('hasURL', () => {
expect(hasURL('this is my personal website: https://www.example.com')).toBe(true) expect(hasURL('this is my personal website: https://www.example.com')).toBe(true)
expect(hasURL('#corngang')).toBe(false) expect(hasURL('#corngang')).toBe(false)
expect(hasURL('Unislap-LP.org')).toBe(true)
expect(hasURL('https://uniswap.org')).toBe(true)
expect(hasURL('https://www.uniswap.org')).toBe(true)
expect(hasURL('http://uniswap.org')).toBe(true)
expect(hasURL('http://username:password@uniswap.org')).toBe(true)
expect(hasURL('http://app.uniswap.org')).toBe(true)
expect(hasURL('username:password@app.uniswap.org:22')).toBe(true)
expect(hasURL('uniswap.org:80')).toBe(true)
expect(hasURL('asdf uniswap.org:80 asdf')).toBe(true)
}) })
export function hasURL(str: string): boolean { export function hasURL(str: string): boolean {
const pattern = new RegExp( const pattern = new RegExp(
'(http|https):\\/\\/' + // Match either "http" or "https" for the protocol '([a-zA-Z0-9]+://)?' + // optional protocol
'(\\w+:{0,1}\\w*)?' + // Allow for an optional username and password in the URL '([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?' + // optional username:password
'(\\S+)' + // Match the domain name or IP address '([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})' + // host name and subdomain
'(:[0-9]+)?' + // Allow for an optional port number in the URL '(:[0-9]+)?(/.*)?' // optional port and path
'(\\/|\\/([\\w#!:.?+=&%!\\-\\/]))?' // Allow for an optional path and query string in the URL
) )
return pattern.test(str) return pattern.test(str)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment