Skip to content

Commit c340227

Browse files
authored
fix(regex): improve url regex (#736)
* fix(regex): improve url regex * test: more unit tests
1 parent cb0ce65 commit c340227

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

packages/regex/src/__tests__/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ const printable =
4949
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
5050
const punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
5151
const sixDigitsCodeTest = '123456'
52-
const url1 = 'http://console.scaleway.com'
53-
const url2 = 'https://www.scaleway.com'
52+
const urls = [
53+
'http://console.scaleway.com',
54+
'https://www.scaleway.com',
55+
'https://www.scaleway.online',
56+
'http://www.scaleway.com:8080',
57+
'http://255.255.255.255',
58+
'http://www.example.com/product',
59+
'http://www.example.com/products?id=1&page=2',
60+
'http://www.example.com#up',
61+
]
5462
const whitespace = ' \t\n\r\x0b\x0c'
5563
const macAddress1 = '1F:B5:FA:47:CD:C4'
5664
const linuxPaths = {
@@ -420,8 +428,7 @@ describe('@regex', () => {
420428
[whitespace, false],
421429
[cronTest, false],
422430
[macAddress1, false],
423-
[url1, false],
424-
[url2, false],
431+
...(urls.map(urlString => [urlString, false]) as [string, boolean][]),
425432
])('should match regex %s to be %s', (string, expected) => {
426433
expect(basicDomain.test(string)).toBe(expected)
427434
})
@@ -604,8 +611,9 @@ describe('@regex', () => {
604611
[whitespace, false],
605612
[cronTest, false],
606613
[macAddress1, false],
607-
[url1, true],
608-
[url2, true],
614+
[domain, false],
615+
[subDomain, false],
616+
...(urls.map(urlString => [urlString, true]) as [string, boolean][]),
609617
])('should match regex %s to be %s', (string, expected) => {
610618
expect(url.test(string)).toBe(expected)
611619
})

packages/regex/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ export const phone = /^\+[0-9]*/
3030
export const spaces = /^\s*$/
3131
export const sixDigitsCode = /^[0-9]{6}$/
3232
export const url =
33-
/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
33+
/^http(s)?:\/\/?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/
3434
export const hexadecimal = /^[0-9a-fA-F]+$/

0 commit comments

Comments
 (0)